Jun 20 2008

Connecting Cairngorm View with the Command Class.

Published by at 6:24 pm under AS 3.0,Flex

I have been playing with Cairngorm quite a bit and usually when i dispatch cairngorm event, i rely on flex data binding with the model class to manage the view. This is applicable in most of the cases but then sometimes its not required to bind some data in your view with the model. 

I came up with this solution to convert my view to a responder and then once data is fetched, I can update my view as required. Its really an easy solution, your view container has to implement mx.rpc.IResponder Interface and then in the cairngorm event you just pass its reference. It looks as follows,

<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml” width=”343” height=”389” show=”initModule()” implements=”mx.rpc.IResponder>

there are only two methods in this interface, result and fault, this is how my script block looks inside this Canvas.

import com.adobe.cairngorm.control.CairngormEventDispatcher;
import com.sampleapp.events.GetStoreItemsEvent;
import com.sampleapp.model.SampleAppModel;
import mx.controls.Alert;

private var model:SampleAppModel;

public function fault(data:Object):void{
}

private function initModule():void{
    if (!SampleAppModel.getInstance().albumDataList){
        model = SampleAppModel.getInstance();
        var event:GetStoreItemsEvent = new GetStoreItemsEvent(GetStoreItemsEvent.GET_STOREITEM);
        event.data = new Object();
        event.data.responderObj = this;
        CairngormEventDispatcher.getInstance().dispatchEvent(event);
    }

}

public function result(data:Object):void{
    var resultXML:XMLList = data.result[0].children().children();
    var finalXML:XML = new XML(resultXML[0].toString());
    model.albumDataList = finalXML.children();
}

and this is how the execute block inside my command class looks like,

public function execute(event:CairngormEvent):void {
    var service:WebService;
    var operation:Operation;
    var token:AsyncToken;
    var requestObj:Object;
    itemType = event.data.itemType;

    service = ServiceLocator.getInstance().getWebService("storeService");

    service.loadWSDL();

    operation = service.getOperation("GetItemsFromStore") as Operation;
    operation.resultFormat = "e4x";
    requestObj = new Object();
    requestObj.itemType = itemType;
    operation.request = requestObj;
    token = operation.send() as AsyncToken;
    //this is the line where the responder object is passed to the Asynctoken
    token.addResponder(event.data.responderObj);

}

so using the above method, We make sure that the result function inside the canvas will be called when the data is received. Now you can do whatever you need to inside this function.

Questions, Comments, Suggestions always welcome.

One response so far

One Response to “Connecting Cairngorm View with the Command Class.”

  1. Rohiton 27 Dec 2015 at 2:05 pm

    It’s been so nice to see you, Luca. I haven’t been able to hang out with you as much as I wanted. Tomorrow I have no sseoisn to teach, so we better hang out!

Trackback URI | Comments RSS

Leave a Reply