I have a slider and a textbox that modify the same variable in my model; so
far so good. Now, how do I synchronize the slider & textbox so that when I change one, the other one updates? In the view's model: method I have: model: aModel super model: aModel. aSlider model: (aModel aspectValue: #aValue). aText model: (aModel aspectValue: #aValue). Thanks for any help! |
Patrick,
> I have a slider and a textbox that modify the same variable in my model; > so far so good. Now, how do I synchronize the slider & textbox so that > when I change one, the other one updates? There are a couple of ways. Using your current framework you will need to ensure that your model has two accessor methods aValue ^whateverYouCalledThe InstVar aValue: anInteger whateverYouCalledTheInstVar := anInteger. self trigger: #wYCTIVChanged The instVar can now be externally read and written to. In addition it now informs any interested party when it's value has been changed. You then have to modify the #model method model: aModel super model: aModel. aSlider model: ((aModel aspectValue: #aValue) aspectTriggers: #wYCTIVChanged). aText model: ((aModel aspectValue: #aValue) aspectTriggers: #wYCTIVChanged) This tells the AspectAdaptor that when the model triggers a #wYCTIVChanged event then it should pass that on by triggering a #valueChanged event. Both aSlider and aText are looking for #valueChanged events to tell them to refresh their contents. The AspectAdaptor is basically just acting as a translator. It has been told what event the object it wrapping triggers when it's value changes and translates that to the #valueChanged event the everyone else is expecting. One other way that is sometimes used ... Model>>initialize super initialize. whateverYouCalledTheInstVar := 0 asValue Model>>whateverYouCalledTheInstVar ^whateverYouCalledTheInstVar Model>>getValue ^whateverYouCalledTheInstVar value Model>>setValue: anInteger ^whateverYouCalledTheInstVar value: anInteger Shell>>model: aModel super model: aModel. aSlider model: aModel whateverYouCalledTheInstVar. aText model: aModel whateverYouCalledTheInstVar This creates a single ValueModel (by sending #asValue in the #initialize method) and uses this ValueModel as the model for all interested parties. A ValueModel defaults to triggering #valueChanged when it is changed so all the triggering is done for you. Regards Ian |
Your first solution works perfectly--thanks! BTW, I was following your first
tutorial and was trying to apply it to my situation, but something I was doing was causing the stack to overflow--I think because my sliders and textboxes also trigger another event & I was confusing the two. Anyway... I have Ted Bracht's Dolphin book on order--I'm hoping it goes into MVP and clears up some of these issues for me. "Ian Bartholomew" <[hidden email]> wrote in message news:qf3R8.10001$VP6.748192@stones... > Patrick, > > > I have a slider and a textbox that modify the same variable in my model; > > so far so good. Now, how do I synchronize the slider & textbox so that > > when I change one, the other one updates? > > There are a couple of ways. Using your current framework you will need to > ensure that your model has two accessor methods > > aValue > ^whateverYouCalledThe InstVar > > aValue: anInteger > whateverYouCalledTheInstVar := anInteger. > self trigger: #wYCTIVChanged > > The instVar can now be externally read and written to. In addition it now > informs any interested party when it's value has been changed. > > You then have to modify the #model method > > model: aModel > super model: aModel. > aSlider model: ((aModel aspectValue: #aValue) aspectTriggers: > #wYCTIVChanged). > aText model: ((aModel aspectValue: #aValue) aspectTriggers: > #wYCTIVChanged) > > This tells the AspectAdaptor that when the model triggers a #wYCTIVChanged > event then it should pass that on by triggering a #valueChanged event. > aSlider and aText are looking for #valueChanged events to tell them to > refresh their contents. The AspectAdaptor is basically just acting as a > translator. It has been told what event the object it wrapping triggers when > it's value changes and translates that to the #valueChanged event the > everyone else is expecting. > > One other way that is sometimes used ... > > Model>>initialize > super initialize. > whateverYouCalledTheInstVar := 0 asValue > > Model>>whateverYouCalledTheInstVar > ^whateverYouCalledTheInstVar > > Model>>getValue > ^whateverYouCalledTheInstVar value > > Model>>setValue: anInteger > ^whateverYouCalledTheInstVar value: anInteger > > Shell>>model: aModel > super model: aModel. > aSlider model: aModel whateverYouCalledTheInstVar. > aText model: aModel whateverYouCalledTheInstVar > > This creates a single ValueModel (by sending #asValue in the #initialize > method) and uses this ValueModel as the model for all interested parties. > ValueModel defaults to triggering #valueChanged when it is changed so all > the triggering is done for you. > > Regards > Ian > > > > > > > > > > > |
Free forum by Nabble | Edit this page |