I wanted to extend on what Charlie said.
If your product value object (vo) is doing the dispatching, then every time
you create a product, it will have to register its listeners. Personally, I
feel this break encapulation, b/c you end it w/vo's being passed about the
application w/way too much power; they are supposed to be "stupid" data
containers that encapsulate your business object (a product)--but they
should not encapsulate business logic (when the user clicks the add button,
do xyz). Arguably, it is kinda "cool" to have products that can add
themselves to a quote, but now the product vo has some sort of implicit
knowledge about the quote sub-system (in that it knows about "addToQuote" as
business logic).
Better to push that knowledge out of the data layer in to the view. Using
Charlie's example:
public function handleClick() : void {
var product : Product = ... // get the currently selected product
dispatchEvent( new AddProductEvent(AddProductEvent.PRODUCT_ADDED, product,
true, false) );
}
<mx;Button click="{handleClick()}"/>
On Mon, Sep 21, 2009 at 6:02 PM, Charlie Hubbard
<[email protected]>wrote:
> So you are trying to dispatch events from your Product object? Your
> Product object doesn't have dispatchEvent() method on it since it doesn't
> extend EventDispatcher, hence the complier message. Instantiating a new
> EventDispatcher instance won't help because nothing is registered to listen
> for events from it. Look at the addToQuote() function. That instance was
> created there, but how could anything outside of that function register a
> listener with it if the instance didn't exist before that call?
> What you have to do is dispatch that event on an EventDispatcher connected
> to the stage. Your Product instance isn't connect to the stage. I would
> say consider dispatching the event from outside your Product class.
> Register a function on that mouse listener, call addToQuote(), then after
> that dispatch an event using the UIs EventDispatcher.
>
> public function handleClick() : void {
> var product : Product = ... // get the currently selected product
> product.addToQuote();
> dispatchEvent( ... );
> }
>
> <mx;Button click="{handleClick()}"/>
>
> I hope that helps, but it's hard to say exactly what you should do given
> I'm not sure who is listening and how they were registered.
>
> Charlie
>
>
> On Mon, Sep 21, 2009 at 5:44 PM, Hepp, Michael W. <[email protected]
> > wrote:
>
>>
>> I know what I am doing wrong must be simple, but I can't for the life of
>> me get this event to fire.
>>
>> In my Product object, when a user clicks the "Add To Quote" button it
>> calls my addToQoute() function:
>>
>> private function addToQuote(event:MouseEvent):void{
>> dispatchEvent( new AddProductEvent(AddProductEvent.PRODUCT_ADDED,
>> true, false) );
>>
>> <some other code>
>> } // END addToQuote()
>>
>> but I get a compile error: 1180: Call to a possibly undefined method
>> dispatchEvent.
>>
>> So I used an eventDispatcher:
>>
>> private function addToQuote(event:MouseEvent):void{
>> var dispatcher:EventDispatcher = new EventDispatcher();
>> dispatcher.dispatchEvent( new
>> AddProductEvent(AddProductEvent.PRODUCT_ADDED, true, false) );
>>
>> <some other code>
>> } // END addToQuote()
>>
>> which seems to call my Event class just fine:
>>
>> package Classes.Events
>> {
>> import flash.events.Event;
>> import mx.collections.ArrayCollection;
>>
>> public class AddProductEvent extends Event {
>>
>> // Define static constant
>> public static const PRODUCT_ADDED:String = "productAdded";
>>
>> public var result:Boolean;
>>
>> public function AddProductEvent(type:String,
>> bubbles:Boolean=false, cancelable:Boolean=false) {
>> // Call the constructor of the superclass.
>> super(type, bubbles, cancelable);
>> }
>> }
>> }
>>
>> but never fires the listener I added to my main app:
>>
>> private function init(event:FlexEvent):void{
>> addEventListener(AddProductEvent.PRODUCT_ADDED, addToQuote);
>>
>> <some other code>
>> } // END init()
>>
>> What am I missing?
>>
>> It's been one of those weeks already!!!!!
>>
>> Mike
>>
>> -------------------------------------------------------------
>> To unsubscribe from this list, simply email the list with unsubscribe in
>> the subject line
>>
>> For more info, see http://www.affug.com
>> Archive @ http://www.mail-archive.com/discussion%40affug.com/
>> List hosted by FusionLink <http://www.fusionlink.com>
>> -------------------------------------------------------------
>
>
>
--
Scott Talsma
CTO, echoEleven