Reputation: 65
I want to know if its a good idea to have an mxml
file for each component, then inline these various components in one mxml
file and able to communicate between these mxml
files ?
For example I have a overView.mxml
and in this, i inline the other mxml
files which contains components like datagrid
, another mxml
contains may be a form. Will i be able to communicate to the grid data by accessing its id in the form mxml
?
Please let me know if my question is not clear. I will try to rephrase it.
Thanks so much.
Upvotes: 1
Views: 431
Reputation: 18193
Yes, this is a good idea. It's much better than creating one giant MXML or class that contains all of your functionality.
At the same time, it caries some overhead. To communicate between the classes (MXML files are just classes) you have to dispatch events. A parent component that contains child components, can set public properties of its child components (with or without data binding).
You should try to decouple your components as much as possible. Try not to write code where the child component is explicitly referring to the parent (ie: try not to use the parent
property). Instead dispatch an event from the child component and make the parent listen for the event.
Regarding your example with a data grid and a form:
If these two objects will have a lot of interaction between them, it will be much easier to keep them in the same MXML file. This doesn't sound like a scenario where one is a parent component that contains a child component. Since these "siblings" are likely to communicate w/each other, it would be easier to have them in the same MXML file. However, if things get complex and the file grows to more than a couple hundred lines, you will likely benefit from separating them.
Let's say you plan to use the form component elsewhere in your project w/out the data grid, then it also makes sense to separate them.
Regarding your question: "will the form component be able to communicate w/the grid component by accessing it's id?":
Technically, yes, you could do that but only by tightly coupling your form component w/the grid component. The form component can use the parent
property to go up one level and use the id of the grid. But since the parent
property is typed as a DisplayObjectContainer
you have to fool the compiler and you loose the benefits of strong typing (because the DisplayObjectContainer
doesn't have a property that is the ID of your grid). So while this is possible, it's not good design and prevents you from reusing the form component elsewhere.
This tight coupling defeats the purpose of creating separate components. So you're back to either facilitating that communication through dispatching events, or putting the grid and form in the same document :)
TLDR:
You should generally be trying to create re-usable components. So I would generally lean in that direction. MXML files are just classes, all the usual things people talk about in object oriented programming apply.
Upvotes: 2