Expand my Community achievements bar.

SOLVED

Using sharedModel for viewstacks?

Avatar

Level 2

Is it possible to use the sharedModel for viewstack components? As an example of what I'm trying to do: I'd like to allow one person to change which particular viewstack is being displayed to all connected users.

So my current code would read something like "myViewStack.selectedChild=viewstack2" after a button is pressed. The problem if I take the route described in video 3 is that I don't know what the children will be after I subclass the viewstack.

Any tips here?

1 Accepted Solution

Avatar

Correct answer by
Level 2

Thanks Nigel--I guess in your example, it would work for using selectedIndex, which you can actually use inline as a property of the Viewstack. But I was hoping to use selectedChild (in case the order of the children changed), and I was reading the API docs, and it said that selectedChild could not be inline, it had to instead be called by an ActionScript block. Here's where I was reading about this: http://livedocs.adobe.com/flex/3/html/help.html?content=navigators_3.html

In my initialize function for my Viewstack, I set the selectedChild to some default value. But I'm not sure how to make it change every time the SelectedViewChild SharedProperty updates. So I can't make the Viewstack's selectedChild={selectedViewChild.value} for example. Is there any way I can use an event listener to make sure the Viewstack changes on all clients using selectedChild instead of selectedIndex? Not sure how else to "bind" the property from the Viewstack to the SharedProperty.

EDIT: so I added an event listener to wait for a change on the shared property, but I am having trouble setting the selectedChild. I get a lot of implicit type coersion errors. Is there a way to just set the selectedChild based on the string of the SharedProperty?

EDIT #2 (Solution): Figured this out, it was mostly a matter of typecasting properly. The idea is that SharedProperty is a string, and you can't use a string when assigning a new selectedChild, so I cast it as a Container and got the child container like this: viewstack.selectedChild = (viewStack.getChildByName( selectedViewStackChild.value) as Container). I had to setup an event listener when the viewstack initializes to detect when the SharedProperty changes, and the viewstack needs a default value so that has to be set.

View solution in original post

4 Replies

Avatar

Former Community Member

A SharedProperty should do the trick here. You could wire it up like (warning, pseudocode) :

protected var selectedViewIndex:SharedProperty = new SharedProperty();

In some init function, you’d have to run this :

selectedViewIndex.sharedID = “selectedViewIndex”; // or whatever you want to name it

selectedViewIndex.subscribe();

MXML code :

<mx:Viewstack selectedIndex={ selectedViewIndex.value}>

</mx:ViewStack>

And then your code on buttonclick would essentially be :

selectedViewIndex.value = 3; // or whatever the selectedIndex you wanted is.

Hope that helps

nigel

Avatar

Correct answer by
Level 2

Thanks Nigel--I guess in your example, it would work for using selectedIndex, which you can actually use inline as a property of the Viewstack. But I was hoping to use selectedChild (in case the order of the children changed), and I was reading the API docs, and it said that selectedChild could not be inline, it had to instead be called by an ActionScript block. Here's where I was reading about this: http://livedocs.adobe.com/flex/3/html/help.html?content=navigators_3.html

In my initialize function for my Viewstack, I set the selectedChild to some default value. But I'm not sure how to make it change every time the SelectedViewChild SharedProperty updates. So I can't make the Viewstack's selectedChild={selectedViewChild.value} for example. Is there any way I can use an event listener to make sure the Viewstack changes on all clients using selectedChild instead of selectedIndex? Not sure how else to "bind" the property from the Viewstack to the SharedProperty.

EDIT: so I added an event listener to wait for a change on the shared property, but I am having trouble setting the selectedChild. I get a lot of implicit type coersion errors. Is there a way to just set the selectedChild based on the string of the SharedProperty?

EDIT #2 (Solution): Figured this out, it was mostly a matter of typecasting properly. The idea is that SharedProperty is a string, and you can't use a string when assigning a new selectedChild, so I cast it as a Container and got the child container like this: viewstack.selectedChild = (viewStack.getChildByName( selectedViewStackChild.value) as Container). I had to setup an event listener when the viewstack initializes to detect when the SharedProperty changes, and the viewstack needs a default value so that has to be set.

Avatar

Former Community Member

Hi,

The problem with selectedChild is that it’s reference-based, rather than value based. This means, to set the selectedChild I have to have a reference to all the children; there’s no way to send a reference to a particular Object across the network and have it be shared, which is why I went with “index”. If you’re using Flex 4+, the new “states” mechanism also avoids this problem (and is much nicer), by letting you shared the name of the current state.

nigel

Avatar

Level 2

I'm currently using Flash Builder 4 (no Designer-mode support for 4.5, which I still like to use to quickly lay things out), but I didn't like that to construct an entire state, you had to programatically add or remove elements. It became a long chain of adding and removing children, so I found viewstacks to work nicer for me.