5 Replies Latest reply on Oct 27, 2010 5:05 AM by Meena Raundhal

    How do I trigger an arrayCollection event?

    SiHoop Level 1

      In the following files I'm passing data from a model to a display. It works fine, but I want to send a message to the display when an arrayCollection gets populated. I know I could create a custom event, but I don't understand why the eventhandler that I've created will not work. Please tell me what I'm doing wrong.

      Thanks.

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="init()">
              <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                      import mx.events.CollectionEvent;
                      import mx.rpc.events.ResultEvent;
                      [Bindable] private var databaseDataArrayCollection:ArrayCollection=new ArrayCollection;
                      [Bindable] private var model:Model=new Model();
                      [Bindable] private var controller:Controller = new Controller();
                      private function init():void{
                          model.databaseDataArrayCollection.addEventListener(CollectionEvent.COLLECTION_CHANGE,chan ge)
                          model = Model.createInstance();
                          controller.getData();
                      }           
                      private function change(e:Event):void{
                          trace("Changed in view")
                      }
                  ]]>
              </mx:Script>       
          <mx:DataGrid dataProvider="{model.databaseDataArrayCollection}" >
              <mx:columns>
                  <mx:DataGridColumn id="firstName" dataField="firstName" headerText="FIRST NAME" />
                  <mx:DataGridColumn id="lastName" dataField="lastName" headerText="LAST NAME" />
              </mx:columns>
          </mx:DataGrid>
      </mx:Application>

       

      Model.as

      package {
          import mx.collections.ArrayCollection;
          [Bindable]
          public class Model{
              public var databaseDataArrayCollection:ArrayCollection=new ArrayCollection();
              private static var model:Model=new Model;
              private static var num:Number=0;
              public function Model(){
              }
              public static function createInstance():Model{
                  num++
                  if(model == null){
                      model = new Model();
                  }
                  return model;
              }
          }
      }

       

      Controller.as

      package {
          import com.simon.cms.UrlString;
         
          import flash.events.Event;
         
          import mx.collections.ArrayCollection;
          import mx.events.CollectionEvent;
          import mx.rpc.events.ResultEvent;
          import mx.rpc.http.HTTPService;
          import mx.utils.StringUtil;

       

      [Bindable]
          public class Controller{
              private var model:Model=new Model();
              public var httpService:HTTPService=new HTTPService;
              private    var login_string:UrlString=new UrlString();
              public function Controller(){
                  model = Model.createInstance();
              }
              public function getData():void{
                  var params:Object = new Object();
                  httpService.url = login_string.login;
                  httpService.method = "GET";
                  httpService.addEventListener(ResultEvent.RESULT, extractData);
                  httpService.send(params)
              }           
              private function extractData(event:ResultEvent):void{
                  httpService.removeEventListener(ResultEvent.RESULT, extractData);   
                  model.databaseDataArrayCollection.addEventListener(CollectionEvent.COLLECTION_CHANGE,chan ge)
                  model.databaseDataArrayCollection= event.result.student.task;
              }
              private function change(e:CollectionEvent):void{
                  trace("Changed in collection")
              }
          }
      }