4
  • I have an object with a variable containing a String.
  • I have a window containing a LabelMorph/TextMorph (or some other Morph that displays Text?).

How do i bind the LabelMorph/TextMorph to the variable, so that the label updates when the String in the variable changes?

  • classic Smalltalk-80 dependent/change/update mechanism?
  • Pharo Announcement framework?
  • something different??

How would i do this? Which Morph should i use?

MartinW
  • 4,966
  • 2
  • 24
  • 60

3 Answers3

7

Simplest is to use an updating String morph:

UpdatingStringMorph on: self selector: #myLabel

This will send #myLabel (or any other message) to self (or any other object) and display it.

codefrau
  • 4,583
  • 17
  • 17
  • 1
    This works well and i didn't know it existed. But it uses the #step method of the Morph to ask for changes each step. Do you also know what a more "observer-pattern-like" solution could look like? – MartinW Mar 21 '13 at 10:16
  • 1
    You can use any of the frameworks, but nothing beats the simplicity of the Morphic stepping mechanism. This is how Morphic is intended to be used. – codefrau Mar 21 '13 at 13:01
  • Seems, i can only accept one answer. Feel both accepted and i voted both answers up :) – MartinW Mar 27 '13 at 18:05
2

This is a solution provided by Benjamin Van Ryseghem on the Pharo Mailinglist:

For this kind of situation, my solution is to use a ValueHolder. Instead of storing your string directly in an instance variable, store it into the ValueHolder.

I tried this in a Workspace:

|string label|

string := 'Wait till i change..' asValueHolder.
label := LabelMorph contents: string contents.
string whenChangedDo: [:newValue | label contents: newValue ].
label openInWindow.
[ 5 seconds asDelay wait. string value: 'I changed :)' ] fork. 
MartinW
  • 4,966
  • 2
  • 24
  • 60
1

Depends on what you want to achieve. You might want to take a look at a way to do it with Glamour in a current Moose image. In a workspace, do-it:

GLMBasicExamples new magritte openOn: GLMMagrittePersonExample sampleData 

That shows how to work with announcements on save. The earlier examples are a better way to start understanding how to work with Glamour (and because of the way the examplebrowser is build, the Magritte example doesn't update the list when it is nested):

GLMBasicExamples open

That has several other examples that update on change.

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65