[*] Hello Snoopy

.NET and Flash Blog
Flash AS2 用EventDispatcher广播事件

 

Product.as文件

import USStore.Model.ProductInfo;
class USStore.BusinessObj.Product
{
    public 
var addEventListener:Function;
    public 
var removeEventListener:Function;
    private 
var dispatchEvent:Function;
    
    public 
function Product() 
    {
        mx.events.EventDispatcher.initialize(
this);
    }
    public function GetProductsByCategoryID(categoryID:Number):Void 
    {
        
var xml:XML = new XML();
        xml.ignoreWhite 
= true;
        xml.load("data/products.xml");
        
var self = this;
        xml.onLoad 
= function(suc) 
        {
            
if(suc) 
            {
                
//产品集合
                var items:Array = new Array();
                
var nodes = this.firstChild.childNodes;
                
for(var i:Number=0; i<nodes.length; i++
                {
                    
var pobj:ProductInfo = new ProductInfo();
                    pobj.ProductID        
= parseInt(nodes[i].attributes.productID);
                    pobj.CategoryID        
= parseInt(nodes[i].attributes.categoryID);
                    pobj.ProductName    
= nodes[i].attributes.productName;
                    pobj.Brief            
= nodes[i].attributes.brief;
                    pobj.Detail            
= nodes[i].attributes.detail;
                    pobj.Image            
= nodes[i].attributes.image;
                    pobj.Price            
= parseInt(nodes[i].attributes.price);
                    pobj.AddDate        
= nodes[i].attributes.marketDate;
                    items[i] 
= pobj;
                    
//trace(pobj.ProductName);
                }
                //广播产品加载成功事件
                var eventObj:Object    = {target:self,type:"Loaded",args:items};
                self.dispatchEvent(eventObj);
            } 
else {
                getURL(
"ErrorPage.htm");
            }
        };
    }
}

fla文件上的as:

stop();
_global.Product 
= new USStore.BusinessObj.Product();
var loadObj:Object = new Object();
loadObj.Loaded 
= function(eventObj)
{
    
var items:Array = eventObj.args;
    
for(var i=0;i<items.length;i++)
    {
        _root.txtShow.text 
+= items[i].ProductName;
    }
};
_global.Product.addEventListener(
"Loaded",loadObj);
_global.Product.GetProductsByCategoryID();

posted on 2004-12-28 20:39  HelloSnoopy  阅读(684)  评论(0)    收藏  举报