Link to home
Start Free TrialLog in
Avatar of ssmacwilliams
ssmacwilliamsFlag for United States of America

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:ResultEvent):void
            {                              
      historyCollection = event.result as ArrayCollection;                  
      var a:LocalClientHistory= new LocalClientHistory();
      a.sethistoryCollection(historyCollection);            
      TranPanel.gridHistory.dataProvider=historyCollection;            
              }      
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;        }		
	}
}

Open in new window

Avatar of zzynx
zzynx
Flag of Belgium image

I don't know if I get you very well.


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 = new LocalClientHistory();

You can call the setter on it to set that variable:

history.sethistoryCollection(historyCollection);

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.getHistoryCollection();

Or that class can add objects to that history:

var newObject : Object = new Object();
history.getHistoryCollection().add( newObject );


In fact:

In class MyClass1 you can have this:

var historyHolder:LocalClientHistory = new LocalClientHistory();
historyHolder.setHistoryCollection(historyCollection);


In class MyClass2 you can have this:

var class1Reference:Class1 = new Class1();
...
var o:Object = class1Reference.getHistoryCollection().get(0); // Gets the 1st item out of the instance of Class1's historycollection

I hope it shed some light on your problem.
Avatar of ssmacwilliams

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.sethistoryCollection(historyCollection);

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:ArrayCollection;


      /**
       * Getter for the singleton instance
       */      
      public static function getInstance():LocalClientHistory{
         if (_instance==null)
               _instance = new LocalClientHistory();
          return _instance;
      }            

     public function LocalClientHistory() {
     }
     public function gethistoryCollection():ArrayCollection {
          return this.historyCollectionr;
     }
     public function sethistoryCollection(hC:ArrayCollection):void {
          this.historyCollectionr=hC;
     }
}

This way there's only one instance of LocalClientHistory!

You get it from whereever via:

var lch : LocalClientHistory = LocalClientHistory.getInstance();

So, if in Class1 you add a 1st entry like this:

LocalClientHistory.getInstance().add(object1);

In another class Class2 you can get that object out like this:

var o:Object = LocalClientHistory.getInstance().get(0);


I hope this helps.
I have to go offline now.
Will be back this evening or otherwise tomorrow.
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:LocalClientHistory.
1061: Call to a possibly undefined method getInstance through a reference with static type components.events:LocalClientHistory.

by using this syntax:
     LocalClientHistory.getInstance().add(event.result as ArrayCollection);
     var a:LocalClientHistory= new LocalClientHistory().getInstance();

Would the add() method be the equivilant as the sethistoryCollection()?
It's coming...I think :)
I believe I have it writing to the LocalClientHistory....YEHAW! with the following...or at least I think it is because it did write populate the datagrid.
    private function find_history_Handler(event:ResultEvent):void
       {                              
             //historyCollection = event.result as ArrayCollection;                  
             LocalClientHistory.getInstance().sethistoryCollection(event.result as ArrayCollection);
             var a:LocalClientHistory=  LocalClientHistory.getInstance();
             TransPanel.gridHistory.dataProvider=a.gethistoryCollection();      
     }      

HOWEVER, when I attempted retrieve it from the other MXML I was unsuccessful using this:

private function initComp():void
     {
          var ba:LocalClientHistory=  LocalClientHistory.getInstance();      
          History=ba.gethistoryCollection() ;
     }

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:ArrayCollection to an unrelated type components.events:LocalClientHistory.

In the class, isn't the LocalClientHistory an ArrayCollection? What am I missing?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
You're welcome ssmacwilliams.
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.