We were facing one problem in our project that one list grid was loading twice. After debugging we found that the we send a notification to load the grid from the view. This notification was listened by mediator. What we noticed that the façade was notifying the mediator about that notification twice that is why the list was loading twice. After digging into the code we found that if your same notification is repeated in the list returned by the listNotificationInterests, then the façade notify your mediator that many time.
Here is sample to test this. In this example there a simple text box, a button and a label. Once you type text and press send now it will send a notification which is listened by the mediator. But the mediator has repeated the notification name while showing interest of listening notification to facade. Now when ever the notification is sent the mediator is notified twice and handleNotification is called twice.
The MainApplication code : PureMVC101.mxml
<?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 org.puremvc.as3.patterns.facade.Facade; import flash.net.sendToURL; var myfacace:MyFacade = MyFacade.getInstance(); private function init():void { myfacace.registerMediator(new MyMediator(this)); } private function handleChange():void { myfacace.sendNotification(MyMediator.TEXT_IS_TEXT,txtInput.text); } ]]> </mx:Script> <mx:VBox> <mx:TextInput text=”Type your text here” id=”txtInput”/> <mx:Button label=”Send Now” click=”handleChange()”/> <mx:Label text=”Yout text will appear here” id=”lblOutput”/> </mx:VBox> </mx:Application>
Application Facade : MyFacade.as
package { import org.puremvc.as3.patterns.facade.Facade; public class MyFacade extends Facade { public function MyFacade() { super(); } public static function getInstance():MyFacade { if (instance == null) { instance = new MyFacade( ); } return instance as MyFacade; } } }
Mediator : MyMediator.as
package { import mx.controls.Alert; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; public class MyMediator extends Mediator { public static const NAME:String = “MyMediator”; public function MyMediator(viewComponent:Object=null) { super(NAME,viewComponent); } public static const TEXT_IS_TEXT:String =”textistext”; override public function listNotificationInterests():Array { return [TEXT_IS_TEXT,TEXT_IS_TEXT]; } override public function handleNotification(notification:INotification):void { switch(notification.getName()) { case TEXT_IS_TEXT : view.lblOutput.text = notification.getBody() as String; Alert.show(“I am calleddddddddd”,”MyMediator”); break; } } protected function get view():PureMVC101 { return viewComponent as PureMVC101; } } }
I hope that pure mvc team will soon rectify this.