PureMVC Architects Lounge

PureMVC Manifold => Standard Version => Topic started by: fidiman on September 23, 2008, 05:09:04



Title: Why my component doesnt dispatch the event ?
Post by: fidiman on September 23, 2008, 05:09:04
Hi,

I've a component view named "ProductForm", it comes with his mediator called "ProductFormMediator".

There are register in the facade with :

facade.registerMediator( new ProductFormMediator( app.productForm ) );

In the component, i dispatch an event for update some fields for a product.( call service, write in DB, etc... and return)

All work perfectly for the update loop.

But now, i'm trying to add a "create product" functionnality.

I copy / paste the code for update and do some renaming.

So, from the productForm, when user click button i do :

:
dispatchEvent( new ProductEvent(ProductEvent.EVENT_CREATE_PRODUCT, currentProduct ))
I have an Alert that show me that currentProduct exist and the ProductEvent exist too.

In the mediator, i listen but nothing happen :

Here's the m├ędiator :

:
package com.freddufau.eshop.view
{
import com.freddufau.eshop.ApplicationFacade;
import com.freddufau.eshop.event.ProductEvent;
import com.freddufau.eshop.model.ProductProxy;
import com.freddufau.eshop.model.vo.Product;
import com.freddufau.eshop.view.components.ProductForm;

import org.puremvc.as3.interfaces.IMediator;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;

import mx.controls.Alert;

public class ProductFormMediator extends Mediator implements IMediator
{

private var productProxy:ProductProxy;

public static const NAME:String = 'ProductFormMediator';


public function ProductFormMediator(viewComponent:Object=null)
{
super(NAME, viewComponent);

// Add Listeners to his view
productForm.addEventListener( ProductEvent.EVENT_UPDATE_PRODUCT, onUpdate );
productForm.addEventListener( ProductEvent.EVENT_CREATE_PRODUCT, onCreate );

productProxy = facade.retrieveProxy(ProductProxy.NAME) as ProductProxy ;
}


public function get productForm():ProductForm{
return viewComponent as ProductForm;
}

private function onUpdate(pEvt:ProductEvent):void
{
productProxy.updateProduct( pEvt.product );
}

private function onCreate( pEvt:ProductEvent ):void
{
Alert.show("onCreate", "infos");    // this alert is neveer showned !!!
productProxy.createProduct( pEvt.product );
}

override public function listNotificationInterests():Array
{
return [
ApplicationFacade.SELECT_PRODUCT

];
}

override public function handleNotification(notification:INotification):void
{

switch ( notification.getName() )
{
case ApplicationFacade.SELECT_PRODUCT:

var product:Product = notification.getBody() as Product;
//Alert.show(notification.toString(), "info");
productForm.product = product ;
break;

}

}

}
}

Here the component view :

:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" xmlns:vo="com.freddufau.eshop.model.vo.*">

<mx:Metadata>
<!--[Event(name="updateProduct", type="com.freddufau.eshop.event.ProductEvent")]
[Event(name="createProduct", type="com.freddufau.eshop.event.ProductEvent")]-->
</mx:Metadata>

<mx:Script>
<![CDATA[
import com.freddufau.eshop.event.ProductEvent;
import com.freddufau.eshop.model.vo.Product;
import mx.controls.Alert;

[Bindable]
public var product:Product;

[Bindable]
public var mode:String = "save";

private function clickHandler():void
{
if( mode == "create" )
{
Alert.show( "dispatch " + currentProduct.toString(), "info"); // this alert is showned
dispatchEvent( new ProductEvent(ProductEvent.EVENT_CREATE_PRODUCT, currentProduct ))
}
else
{
dispatchEvent( new ProductEvent(ProductEvent.EVENT_UPDATE_PRODUCT, currentProduct)) // here it work !
}

}
]]>
</mx:Script>

<vo:Product id="currentProduct"
id_product="{product.id_product}"
nom="{nomTi.text}"
prix="{int(prixTi.text)}"

/>

<mx:Form x="10" y="10" defaultButton="{saveBt}">

<mx:FormItem label="nom">
<mx:TextInput id="nomTi" text="{product.nom}"/>
</mx:FormItem>

<mx:FormItem label="prix">
<mx:TextInput restrict="0123456789-" id="prixTi" text="{product.prix}"/>
</mx:FormItem>



<mx:FormItem id="saveBt">
<mx:Button label="{mode}" click="clickHandler()"/>
</mx:FormItem>
</mx:Form>

</mx:Canvas>

thanks for your help !


Title: Re: Why my component doesnt dispatch the event ?
Post by: puremvc on September 23, 2008, 08:30:03
You failed to show us the source for ProductEvent. Does it have its bubble property set to true? Is the value of constant EVENT_CREATE_PRODUCT inadvertantly the same as EVENT_UPDATE_PRODUCT?

-=Cliff>


Title: Re: Why my component doesnt dispatch the event ?
Post by: fidiman on September 24, 2008, 12:09:52
Hi Cliff,

here's productEvent

:
package com.freddufau.eshop.event
{
import flash.events.Event;
import com.freddufau.eshop.model.vo.Product;
import flash.events.Event;

public class ProductEvent extends Event
{

public static const EVENT_GET_ALL_PRODUCTS:String='getAllProduct';
public static const EVENT_GET_PRODUCT:String='getProduct';
public static const EVENT_CREATE_PRODUCT:String='createProduct';
public static const EVENT_DO_CREATE_PRODUCT:String='doCreateProduct';
public static const EVENT_SHOW_PRODUCT_FORM:String='showProductForm';
public static const EVENT_UPDATE_PRODUCT:String='updateProduct';
public static const EVENT_DELETE_PRODUCT:String='deleteProduct';
public static const EVENT_SELECT_PRODUCT:String='selectProduct';



public var product:Product;

public function ProductEvent (pType:String, pProduct:Product=null){

product= pProduct;
super(pType);

}

override public function clone():Event{

return new ProductEvent(type, product);

}


}

}


Title: Re: Why my component doesnt dispatch the event ?
Post by: fidiman on September 24, 2008, 12:27:52
hum, i've in the facade some event that are same string value those in product event ?

conflict ?


:
package com.freddufau.eshop
{
import com.freddufau.eshop.control.command.*;
import com.freddufau.eshop.view.components.MainView;

import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;

public class ApplicationFacade extends Facade implements IFacade
{

public static const APP_STARTUP:String = "AppStartUp";
public static const CREATE_PRODUCT:String = "createProduct";
public static const SHOW_CREATE_FORM:String = "showCreateForm";
public static const GET_PRODUCTS:String = "getProducts";
public static const UPDATE_PRODUCT:String = "updateProduct";
public static const DELETE_PRODUCT:String = "deleteProduct";
public static const SELECT_PRODUCT:String = "setProduct";
public static const PRODUCT_DELETED:String = "productDeleted";

public static var MAIN_VIEW:MainView;


public static function getInstance() : ApplicationFacade {

if ( instance == null ) instance = new ApplicationFacade( );

return instance as ApplicationFacade;

}

override protected function initializeController():void
{
super.initializeController();

registerCommand(APP_STARTUP, StartUpCommand);
// Uncomment and replace with the appropriate Commands
//registerCommand(CREATE_PRODUCT, CreateProductCommand);
//registerCommand(GET_PRODUCTS, GetProductsCommand);
//registerCommand(UPDATE_PRODUCT, UpdateProductCommand);
//registerCommand(DELETE_PRODUCT, DeleteProductCommand);


}

public function startup( app:MainView ):void
{
MAIN_VIEW = app;
sendNotification( ApplicationFacade.APP_STARTUP, app ) ;
}

}
}


Title: Re: Why my component doesnt dispatch the event ?
Post by: fidiman on September 24, 2008, 01:20:16
yes !

it's work, after some changes, it work using the popmanager that you show me.

thanks again.