0

所以我在尝试将产品类型添加到集合之前尝试从 json 文件中存储产品类型,但得到了一些奇怪的结果(因为我不完全理解)

在我的路由器页面上,我为缓存的产品和产品类型设置了一个变量

cachedProductTypes: null,
productType : {}, 
products : {},  

getProductTypes:
        function(callback)
        {
            if (this.cachedProductTypes !== null) {
                return callback(cachedProductTypes);
            }

            var self = this;

            $.getJSON('data/product.json',
                function(data)
                {
                    self.cachedProductTypes = data;
                    callback(data);
                }
            );
        },

parseResponse : function(data) {


          result = { prodTypes: [], products: [] };

          var type;
          var types = data.data.productTypeList;
          var product;
          var i = types.length;


         while (type = types[--i]) {
            result.prodTypes.push({ 
               id: type.id,
               name: type.name,
               longName: type.longName
               // etc.
            });
            while (product = type.productList.pop()) {
              product.productTypeId = type.id,
              result.products.push(product);
            }   
          };

          this.productType = result.prodTypes;
          console.log( "dan");
          this.products = result.products;      
    },
showProductTypes:function(){
        var self = this;

        this.getProductTypes(
            function(data)
            {
                self.parseResponse(data);

     var productTypesArray = self.productType;
     var productList=new ProductsType(productTypesArray);   
     var productListView=new ProductListView({collection:productList});

     productListView.bind('renderCompleted:ProductsType',self.changePage,self); 
     productListView.update();
    }
        );      
    }

当用户进入显示产品类型页面时,它会运行 showProductsType 函数

所以我将产品类型数组传递给我的集合

在收藏页面

      var ProductsType=Backbone.Collection.extend({

      model:ProductType,

      fetch:function(){
        var self=this;
        var tmpItem;
        //fetch the data using ajax



        $.each(this.productTypesArray, function(i,prodType){

            tmpItem=new ProductType({id:prodType.id, name:prodType.name,    longName:prodType.longName});
            console.log(prodType.name);
            self.add(tmpItem);
        });

        self.trigger("fetchCompleted:ProductsType");

      }
}); 
return ProductsType;

现在这不起作用,因为如果我 console.log 它 this.productTypesArray 是未定义的。(我应该怎么得到这个?)

我原以为我需要检查并添加每个新的 ProductType。

奇怪的一点 - 如果我只有代码

    var ProductsType=Backbone.Collection.extend({

      model:ProductType,

      fetch:function(){
        var self=this;
        var tmpItem;
        //fetch the data using ajax


        self.trigger("fetchCompleted:ProductsType");

      }
});


return ProductsType;

it actually adds the products to the collection? I guess this means I can just pass an array to the collection and do not have to add each productType?

4

1 回答 1

1

I guess this means I can just pass an array to the collection and do not have to add each productType?

Yes, you can pass an array to the collection's constructor, and it will create the models for you.

As far as your caching code, it looks like the problem is here:

if (this.cachedProductTypes !== null) {
    return callback(cachedProductTypes);
}

The callback statement's argument is missing this - should be return callback(this.cachedProductTypes).

于 2012-12-10T01:00:53.920 回答