William Spence wrote:
I am trying a memory technique suggested by Adobe where if I want to store data and share it among components, I can wrap the data in a custom event class.
I'd like to see this suggestion - where is it? Or do you just mean event handling in the normal manner?
The problem I am having has to do with how long a dispatched event lasts. Say I have a component that I want to store and share data from, so I wrap the data in an event class and dispatch it. The component that I want to share the data with has not been instantiated yet until further in the programs life cycle. When that component finally is instantiated and a listener is created to catch the event, will the event still exist and the data still waiting? If not, how can I get early component data shared with a component that lives down the line?
You really can't use this idea. Once the event is despatched, propogated and processed it's gone. You can't pick up the event after that and you have little opportunity to add more things into the display list.
I really can't understand why you would even contemplate this - I have never heard of anything remotely like this scheme being proposed before.
What's the problem with storing the data by the usual means rather than as a transient data packet?
Sorry Paul, just learning as I go. I am really new to programming and Flex. I am trying to figure out a way of passing data between components and am having trouble because the components don't exist on the stage at the same time. Here is the link that I found where Adobe is suggesting extending the Event Class to store and pass data. If you have a better solution for moving data between components, I would really like to hear it, I am all ears. Thanks for any help!
We all start somewhere, so no worries.
The link you gave just describes regular custom events and is not intended or suitable for the purpose that you suggest - basically acting as a storage mechanism betweeen a component that exists and one that does not yet exist.
Reading between the lines, I guess that you are trying to pass the parcel (which is all events really do) between UI components, rather than have a seperate data model to which UI components refer.
Perhaps you can describe your situation in more depth.
Basically, I am just interested in the basic mechanisms of how to store and pass important values and objects between components.
As a simple example, in my application, my user clicks a button to start a new project. A folder is created with a name based on the date and time to put all future image assets into. Later in the program's life cycle, I need to create the image thumbnails that you and I have discussed previously. I need to know the name and path of the folder that was created in the earlier component to put these image items into. I will have many many instances of things like this happening, so I was looking for the best way to store the data and share it between my components.
Eventually, I am going to be using SQLite to store a lot of information, but as the learning curve is high for me right now, I was looking for a more simple solution until that became necessary. (Don't want to have to learn SQL right now as I am still tripping all up and down ActionScript and MXML)
What is the standard way, besides a database, that users usually store and pass data bewtween components in Flex? Thanks!
I read some about Value Objects last night and thought that was the answer to my problem, but I couldn't figure out how to share the objects between the components.
What you are missing is a data model which stores the data that the applications UI is based upon. You need to research the MVC architecture.
The basic premise is this.
The UI interacts with the user displaying data from the model and updating the model according to the interactions of the user, so generally speaking what the user sees in the UI is a representation of the data that is in the model not data held in the UI. An example might be a set of sales figures. One UI component might show the data using a datagrid while another part of the UI might show it as a pie chart or histogram, etc. The UI components do not refer to each other, but only show what is stored in the model. In Flex, the UI often uses data binding onto the model, to obtain the required data to display.
So, the model is the core of the application, and the UI is only there to visualise the model and for the user to update the model. The UI and model are seperate.
The model can be as simple as a class that can be accessed by all UI components. A popular way to access the data from anywhere is via the singleton pattern, which allows the UI to access the data without declaring the class.
The final part of MVC is using events to communicate UI changes to the model. This is done using a controller, which picks up UI events and updates the model according to what is going on in the UI.
This is all a lot to take in.
Thanks Paul! Sometimes it just takes someone to light the way, and you can figure it out while you trip about. Thanks for pointing me in the right direction. I will research MVC and Singleton tonight and post if I have any problems.
> Once the event is despatched, propogated and processed it's gone. You can't pick up the event after that ...
I'm not sure what you meant by "processed", but I wanted to clarify that you CAN access the Event instance after it has been propogated to the listeners by dispatchEvent(). For example, a common pattern is
var event = new Event("foo");
This code checks whether any of the listeners called event.preventDefault() on the Event instance.
There is nothing special about Event instances, and they have the same lifetime as instances of any other class. In the case above, the instance would be eligible for garbage collection when the function f returns, because the only reference to it was in a local variable of that function.
Adobe Flex SDK Team
Gordon, thanks for your reply. Do you have any comments on what is the most common practice of storing and passing data between components? Do most use value objects, custome classes, custome event objects, singletons? Any insight would be greatly appreciated. This is the biggest hangup that I am having in my introduction to Flex - values that are created in one component that will be needed at later times by other components and how to store and pass those values. You can read earlier in this thread an example of what I am trying to accomplish. Thanks.
values that are created in one component that will be needed at later times by other components
This is the Model in the Model-View-Controller applicaton architecture that pauland referred to. Most models these days probably use value objects, but you could also consider using XML or the <fx:Model> tag. The Flex framework hasn't standardized on a single way for an application to store or access its model, although some I believe various app frameworks built on top of Flex do. But a singleton root to a tree of value objects is a simple approach that should work fine.