ssmacwilliams
asked on
How do I pass data between components
I need a better understanding of passing data in classes in general, but specifically FLEX.
I have created a class and the class hold values that I need to access throughout the application to either update, view, deltete, etc
I need to know how to access values from within different MXML files and set from within different MXML files.
What I have assumed:
I have figured out that I import this in the the MXML inorder to access this class
I also of been able to set the historyCollectionr with the following function:
private function find_history_Handler(event :ResultEve nt):void
{
historyCollection = event.result as ArrayCollection;
var a:LocalClientHistory= new LocalClientHistory();
a.sethistoryCollection(his toryCollec tion);
TranPanel.gridHistory.data Provider=h istoryColl ection;
}
I haven't figured out how to retrieve that information from a different component. I have seen a great number of tutorials, but none that really give me the understanding I need...sure I have tested them and they work, and I could cut and paste, but that is not what I want.
I think I have almost there, but not sure. Please help.
I have created a class and the class hold values that I need to access throughout the application to either update, view, deltete, etc
I need to know how to access values from within different MXML files and set from within different MXML files.
What I have assumed:
I have figured out that I import this in the the MXML inorder to access this class
I also of been able to set the historyCollectionr with the following function:
private function find_history_Handler(event
{
historyCollection = event.result as ArrayCollection;
var a:LocalClientHistory= new LocalClientHistory();
a.sethistoryCollection(his
TranPanel.gridHistory.data
}
I haven't figured out how to retrieve that information from a different component. I have seen a great number of tutorials, but none that really give me the understanding I need...sure I have tested them and they work, and I could cut and paste, but that is not what I want.
I think I have almost there, but not sure. Please help.
// here is the Class file I am trying to access
package components.events
{
import mx.collections.ArrayCollection;
public class LocalClientHistory
{
public var historyCollectionr:ArrayCollection;
public function LocalClientHistory()
{ }
public function gethistoryCollection():ArrayCollection
{ return this.historyCollectionr; }
public function sethistoryCollection(hC:ArrayCollection):void
{ this.historyCollectionr=hC; }
}
}
ASKER
I think that you halfway understand or vica verca.
CORRECT: You have a class LocalClientHistory that has a setter for its internal variable 'historyCollection' and a getter.
CORRECT: Well, if you have an instance 'history' of that class:
CORRECT: var history:LocalClientHistory = new LocalClientHistory();
CORRECT: You can call the setter on it to set that variable:
CORRECT: history.sethistoryCollecti on(history Collection );
This is where I miss the boat.. I think
CORRECT/SORT OF: And after that some other class that has a reference to the same localClientHistory instance 'history' can call the getter to get that variable again:
How does the instance of 'history' accessed. I tried what you listed and I would typically get an error like: 1120: Access of undefined property history.
I have on MXML file that encapsulates 3 other MXML files(Components) into it. For simplicity, let's say these 3 other Components would need to share the 'history' instance(update, share, delete, etc). Right now I get the undefined property error. I thought by importing the LocalClientHistory class I would be able to access that data for binding or whatever, but I believe I am not correctly understanding the process.
CORRECT: You have a class LocalClientHistory that has a setter for its internal variable 'historyCollection' and a getter.
CORRECT: Well, if you have an instance 'history' of that class:
CORRECT: var history:LocalClientHistory
CORRECT: You can call the setter on it to set that variable:
CORRECT: history.sethistoryCollecti
This is where I miss the boat.. I think
CORRECT/SORT OF: And after that some other class that has a reference to the same localClientHistory instance 'history' can call the getter to get that variable again:
How does the instance of 'history' accessed. I tried what you listed and I would typically get an error like: 1120: Access of undefined property history.
I have on MXML file that encapsulates 3 other MXML files(Components) into it. For simplicity, let's say these 3 other Components would need to share the 'history' instance(update, share, delete, etc). Right now I get the undefined property error. I thought by importing the LocalClientHistory class I would be able to access that data for binding or whatever, but I believe I am not correctly understanding the process.
>> let's say these 3 other Components would need to share the 'history' instance
So, you want that to be a singleton.
public class LocalClientHistory
{
private static var _instance : LocalClientHistory;
private var historyCollectionr:ArrayCo llection;
/**
* Getter for the singleton instance
*/
public static function getInstance():LocalClientH istory{
if (_instance==null)
_instance = new LocalClientHistory();
return _instance;
}
public function LocalClientHistory() {
}
public function gethistoryCollection():Arr ayCollecti on {
return this.historyCollectionr;
}
public function sethistoryCollection(hC:Ar rayCollect ion):void {
this.historyCollectionr=hC ;
}
}
This way there's only one instance of LocalClientHistory!
You get it from whereever via:
var lch : LocalClientHistory = LocalClientHistory.getInst ance();
So, if in Class1 you add a 1st entry like this:
LocalClientHistory.getInst ance().add (object1);
In another class Class2 you can get that object out like this:
var o:Object = LocalClientHistory.getInst ance().get (0);
I hope this helps.
I have to go offline now.
Will be back this evening or otherwise tomorrow.
So, you want that to be a singleton.
public class LocalClientHistory
{
private static var _instance : LocalClientHistory;
private var historyCollectionr:ArrayCo
/**
* Getter for the singleton instance
*/
public static function getInstance():LocalClientH
if (_instance==null)
_instance = new LocalClientHistory();
return _instance;
}
public function LocalClientHistory() {
}
public function gethistoryCollection():Arr
return this.historyCollectionr;
}
public function sethistoryCollection(hC:Ar
this.historyCollectionr=hC
}
}
This way there's only one instance of LocalClientHistory!
You get it from whereever via:
var lch : LocalClientHistory = LocalClientHistory.getInst
So, if in Class1 you add a 1st entry like this:
LocalClientHistory.getInst
In another class Class2 you can get that object out like this:
var o:Object = LocalClientHistory.getInst
I hope this helps.
I have to go offline now.
Will be back this evening or otherwise tomorrow.
ASKER
Ok this is sort of helping...
I see that you have established a class and within the class you have it create an instance of itself IF there isn't one already there. Then assuming that the MXML adds it using the syntax you provided I should be able to call that LocalClientHistory
and retrieve the data
and then pass it to a dataprovider or manipulate someway, etc
but I get the following errors:
1061: Call to a possibly undefined method add through a reference with static type components.events:LocalCli entHistory .
1061: Call to a possibly undefined method getInstance through a reference with static type components.events:LocalCli entHistory .
by using this syntax:
LocalClientHistory.getInst ance().add (event.res ult as ArrayCollection);
var a:LocalClientHistory= new LocalClientHistory().getIn stance();
Would the add() method be the equivilant as the sethistoryCollection()?
I see that you have established a class and within the class you have it create an instance of itself IF there isn't one already there. Then assuming that the MXML adds it using the syntax you provided I should be able to call that LocalClientHistory
and retrieve the data
and then pass it to a dataprovider or manipulate someway, etc
but I get the following errors:
1061: Call to a possibly undefined method add through a reference with static type components.events:LocalCli
1061: Call to a possibly undefined method getInstance through a reference with static type components.events:LocalCli
by using this syntax:
LocalClientHistory.getInst
var a:LocalClientHistory= new LocalClientHistory().getIn
Would the add() method be the equivilant as the sethistoryCollection()?
ASKER
It's coming...I think :)
I believe I have it writing to the LocalClientHistory....YEHA W! with the following...or at least I think it is because it did write populate the datagrid.
private function find_history_Handler(event :ResultEve nt):void
{
//historyCollection = event.result as ArrayCollection;
LocalClientHistory.getInst ance().set historyCol lection(ev ent.result as ArrayCollection);
var a:LocalClientHistory= LocalClientHistory.getInst ance();
TransPanel.gridHistory.dat aProvider= a.gethisto ryCollecti on();
}
HOWEVER, when I attempted retrieve it from the other MXML I was unsuccessful using this:
private function initComp():void
{
var ba:LocalClientHistory= LocalClientHistory.getInst ance();
History=ba.gethistoryColle ction() ;
}
I thought that this would work, but the following error came about on the calling MXML:
1067: Implicit coercion of a value of type mx.collections:ArrayCollec tion to an unrelated type components.events:LocalCli entHistory .
In the class, isn't the LocalClientHistory an ArrayCollection? What am I missing?
Thanks
I believe I have it writing to the LocalClientHistory....YEHA
private function find_history_Handler(event
{
//historyCollection = event.result as ArrayCollection;
LocalClientHistory.getInst
var a:LocalClientHistory= LocalClientHistory.getInst
TransPanel.gridHistory.dat
}
HOWEVER, when I attempted retrieve it from the other MXML I was unsuccessful using this:
private function initComp():void
{
var ba:LocalClientHistory= LocalClientHistory.getInst
History=ba.gethistoryColle
}
I thought that this would work, but the following error came about on the calling MXML:
1067: Implicit coercion of a value of type mx.collections:ArrayCollec
In the class, isn't the LocalClientHistory an ArrayCollection? What am I missing?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks you have definitely help me a GREAT deal not just in solution but in explanaition. I hope you don't ever decide to leave EE, it truly would be a loss.
I have another question that I think would yield to your expertise. Regarding refreshing/requerying the data that is associated with this question, but I wish give the points... hopefully you have the time and desire. Thanks again
I have another question that I think would yield to your expertise. Regarding refreshing/requerying the data that is associated with this question, but I wish give the points... hopefully you have the time and desire. Thanks again
You're welcome ssmacwilliams.
Thanx 4 axxepting
Thanx 4 axxepting
>> I have another question that I think would yield to your expertise.
Feel free to post a link to that new question in this thread whenever it is ready.
Feel free to post a link to that new question in this thread whenever it is ready.
You have a class LocalClientHistory that has a setter for its internal variable 'historyCollection' and a getter.
Well, if you have an instance 'history' of that class:
e.g.
var history:LocalClientHistory
You can call the setter on it to set that variable:
history.sethistoryCollecti
And after that some other class that has a reference to the same localClientHistory instance 'history' can call the getter to get that variable again:
var coll:ArrayCollection = history.getHistoryCollecti
Or that class can add objects to that history:
var newObject : Object = new Object();
history.getHistoryCollecti
In fact:
In class MyClass1 you can have this:
var historyHolder:LocalClientH
historyHolder.setHistoryCo
In class MyClass2 you can have this:
var class1Reference:Class1 = new Class1();
...
var o:Object = class1Reference.getHistory
I hope it shed some light on your problem.