0

因此,我正在关注 Michael Labriola、Jeff Tapper 和 Matthew Boles 所著的 Adob​​e Flex 4 Training From the Source 一书作为上下文。

我正在构建一个购物车类,它从 mxml 接收一个 ShoppingCartItem 对象(它只是一个 POAO),并通过这个公共函数将它添加到一个 ArrayCollection 中:

private var $items:ArrayCollection = new ArrayCollection();
public function addItem(item:ShoppingCartItem):void
    {
        var inCart:Boolean = false;
        var currentItem:ShoppingCartItem;
        for(var i:int = 0; i < $items.length; i++)
        {
            currentItem = $items.getItemAt(i) as ShoppingCartItem;
            if(item.$product == currentItem.$product)
            {
                inCart = true;
                break;
            }
        }
        if(inCart)
        {
            currentItem.$quantity++;
        }
        else
        {
            $items.addItem(item);
        }
        updateTotal();
        $items.refresh();
    }

根据这本书,可以使用 IViewCursor 实现相同的功能,就像这样。

public function addItem(item:ShoppingCartItem):void
{
   var cursor:IViewCursor = $items.createCursor();
   var inCart:Boolean = cursor.findFirst(item);
   if(inCart)
   {
      var existing:ShoppingCartItem = cursor.current as ShoppingCartItem;
      existing.$quantity++;
   }
   else
   {
      $items.addItem(item)
   }
}

问题是,当我使用此功能时,项目数量永远不会更新。然后我有一个购物车,其中包含 1 个产品的 2 个条目,而我应该有 2 个产品的 1 个条目。无论我做什么,跟踪 inCart 布尔值都会产生“假”。第一个功能按预期正常工作,所以我不知道为什么数据没有正确更新。另外,如果我$items.refresh();在第二个函数的末尾调用(用于排序),它会抛出一个NullPointerException error.

另一件需要注意的是,当我使用 4.6.0 时,我正在使用 Flex 4 的书。SDK,在它被赠予 Apache 之前的最后一个 Adob​​e 版本。我不知道这是否重要。

这是 ShoppingCartItem 的代码:

    [Bindable]
public class ShoppingCartItem
{
    public var $product:Product;
    public var $quantity:uint;
    public var $subtotal:Number;
    public function getSubTotal():Number
    {
        calculateSubTotal();
        return $subtotal;
    }
    public function toString():String
    {
        return "[ShoppingCartItem]"+$product.prodName+":"+$quantity;
    }
    public function calculateSubTotal():void
    {
        this.$subtotal = $product.listPrice*$quantity;
    }
    public function squeak():void
    {
        trace("squeak");
    }
    public function ShoppingCartItem(product:Product, quantity:uint = 1)
    {
        this.$product = product;
        this.$quantity = quantity;
        calculateSubTotal();
    }

编辑:Sunil D 要求提供更多信息。

Product.as 类:

[Bindable]
public class Product
{
    public var catID:Number;
    public var prodName:String;
    public var unitID:Number;
    public var cost:Number;
    public var listPrice:Number;
    public var description:String;
    public var isOrganic:Boolean;
    public var isLowFat:Boolean;
    public var imageName:String;
    public function toString():String
    {
        return "[Product]"+this.prodName;
    }
    public static function buildProductFromAttributes(data:XML):Product
    {
        var p:Product;
        var isOrganic:Boolean = (data.@isOrganic == "Yes");
        var isLowFat:Boolean = (data.@isLowFat == "Yes");
        p = new Product(data.@catID,
                        data.@prodName,
                        data.@unitID,
                        data.@cost,
                        data.@listPrice,
                        data.@description,
                        isOrganic,
                        isLowFat,
                        data.@imageName);
        return p;
    }
    public static function buildProduct(o:Object):Product
    {
        var p:Product;
        p = new Product(o.catId,o.prodName,o.unitID,o.cost,o.listPrice,
                        o.description,(o.isOrganic == 'true'),
                       (o.isLowFat == 'true'),o.imageName);

        return p;
    }
    public function Product(cid:Number, name:String, uid:Number, cost:Number, listp:Number, desc:String, iso:Boolean, ilf:Boolean, imn:String)
    {
        this.catID = cid;
        this.prodName = name;
        this.unitID = uid;
        this.cost = cost;
        this.listPrice = listp;
        this.description = desc;
        this.isOrganic = iso;
        this.isLowFat = ilf;
        this.imageName = imn;
    }
}

ArrayCollection sorting sortfield is the Product POAO contained in the ShoppingCartItem Class. It's done within the constructor function of ShoppingCart like this:

public class ShoppingCart
{
    [Bindable]
    private var $items:ArrayCollection = new ArrayCollection();
    public function ShoppingCart()
    {
        var prodSort:Sort = new Sort();
        var sortField:SortField = new SortField("product");
        prodSort.fields =[sortField];
        $items.sort = prodSort;
        $items.refresh();
    }
4

1 回答 1

-1

The reason the viewcursor approach is not working is because the item (ShoppingCartItem) is not in the $items collection. This method compares object references and will hence not find your item if it is another instance of ShoppingCartItem.

In the first approach you are not comparing object references of ShoppingCartItem, but comparing the "product" property of the items.

于 2013-08-07T13:26:20.930 回答