Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: View updates itself -- should this be avoided?  (Read 8770 times)
99miles
Jr. Member
**
Posts: 14


View Profile WWW Email
« on: November 11, 2009, 11:35:45 »

A form contains a ComboBox populated with state names.

When the selection is changed, statesCombo.selectedItem might give me:
<state>
  <name>
    alabama
  </name>
  <cities>
    <city>
      auburn
    </city>
    <city>
      birmingham
    </city>
  </cities>
</state>

So I have all the city names there which I need to now populate the citiesCombo.

I can do so within the view by simply:
citiesCombo.dataProvider = XML(statesCombo.selectedItem).cities;

but should that be avoided?

Otherwise I guess I would send a notification that is caught by the mediator (no need for a command, right?) that would be passed the state name and the mediator would do the work to get the cities? Seems unnecessary since I already have them, but...

Thoughts?
Thanks.


Logged
coulix
Full Member
***
Posts: 23


View Profile Email
« Reply #1 on: November 11, 2009, 12:13:07 »

I would keep this one on view side since it does not come from the action of a notification nor imply some change on a proxy.

I may be wrong.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: November 11, 2009, 03:09:16 »

Is the question whether the XML should be defined in the view? If this is the only place it's used, you might want to do that to just bake it in where its used and forget about it.

However if it's a large amount of data (adding to the size of your app when it's 'baked in'), and you don't need it until later when the user navigates to the page with this combo, then you might want to put it in an XML file that can be lazy-loaded, then spoonfed to the component by the mediator.

-=Cliff>
Logged
99miles
Jr. Member
**
Posts: 14


View Profile WWW Email
« Reply #3 on: November 11, 2009, 03:37:35 »

Is the question whether the XML should be defined in the view?

Not exactly. My StatesMediator binds the statesArray to the comboBox. The comboBox then displays just the state names. But since I bound the full XML (including the cities for each state), I can get the cities for the selected state within the view by simply saying:

citiesCombo.dataProvider = XML(statesCombo.selectedItem).cities.city as XMLList;

So, my question is: is it bad to be doing that work on the data within the view, or should I implement something else so that the mediator tells the citiesCombo what it's data should be?

Hopefully that's more clear.
Thanks, all.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #4 on: November 12, 2009, 08:30:47 »

Gotcha.

No, it is not bad doing that work in the view component. A View Component is not a 'dumb' thing. It can do whatever it needs internally in order to take the data provided to it and get its groove on. This is an example of local wiring that is the perfect place for Flex binding.

Also, I wouldn't have a Mediator just for the combo box (too fine-grained), or let the Mediator for the component the combos are a part of have access to those combos. The Mediator doesn't need to know the internals of the view component.

So I'd have the custom component that the combos are part of expose a bindable public variable called statesArray (I gather you have an Array of XML objects). Then have the states combo box bind to that. Then the cities combo would bind to the expression that gets the cities.

:
<mx:Script>
<![CDATA[

     [Bindable] public var statesArray:Array;

]]>
</mx:Script>

<mx:ComboBox id="statesCombo" dataProvider="{statesArray}"/>
<mx:ComboBox id="citiesCombo" dataProvider="{XML(statesCombo.selectedItem).cities.city as XMLList}"/>

Now all the mediator does is set statesArray on the view component and binding does the rest internally to populate the states combo and automagically repopulate the cities combo on each selection.

-=Cliff>
Logged
99miles
Jr. Member
**
Posts: 14


View Profile WWW Email
« Reply #5 on: November 13, 2009, 10:07:22 »

So helpful Cliff, thanks. I'm slowly but surely "getting it".
Logged
Pages: [1]
Print