質問

So I'm following the book Adobe Flex 4 Training From the Source by Michael Labriola, Jeff Tapper and Matthew Boles, for context.

I'm building a Shopping Cart Class that recieves a ShoppingCartItem object (which is just a POAO) from the mxml and adds it to an ArrayCollection via this public function:

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();
    }

According to the book, the same function can be achieved with an IViewCursor, like this.

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)
   }
}

Problem is, when I use this function the item quantity is never updated. Then I have a Shopping cart with 2 entries of 1 product when I should have 1 entry of 2 products. Tracing the inCart boolean yields "false", no matter what I do. The first function works properly and as expected, so I have no idea why the data is not being updated correctly. Also, if I call $items.refresh(); at the end of the second function (for sorting), it throws a NullPointerException error.

Another thing to notice is that I'm using a book for Flex 4 when I'm using the 4.6.0. SDK, the last Adobe release before it was gifted to Apache. I don't know if this is of any importance.

Here's the code for 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();
    }

EDIT: More information request by Sunil D.

Product.as class:

[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();
    }
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top