2012-03-27 37 views
0

我的這段代碼不斷拋出錯誤,我不明白爲什麼。集合不是一個函數或集合不是主幹中的構造函數

var id = $(ev.currentTarget).data("id"); 
    var item = ItemCollection.getByCid(id); 
    alert(item.get("ItemCode")); 
    var qty = 1; 
    var cartcollection = new CartCollection(); 
    cartcollection.add(item); 
    CartListView.render(); 
    var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")}); 
    if(itemcode.length > 0){ alert("success"); } 

所以我想要做的是檢查,如果CartCollection具有相同的模型已經並且如果爲真,應該只更新模型的數量atrib。現在基於該代碼它返回CartCollection不是一個函數或不是一個構造函數。爲什麼是這樣!?有任何想法嗎?由於

更新

我使用的骨幹,要求,KendoUI網格和在這一個強調所以我的代碼是這樣的:

itemlist_view.js

define([ 
'jquery', 
'underscore', 
'backbone', 
'model/item_model', 
'model/cart_model', 
'collection/item_collection', 
'collection/cart_collection', 
'view/cart/cartlist_view', 
'text!templates/items/itemlist.html' 
],function($, _, Backbone, Item, Cart, ItemCollection, CartCollection, CartListView, ItemListTemplate){ 

var ItemListView = Backbone.View.extend({ 
el: $("#mainContainer"), 
events:{ 
    "click #itemListContainer li" : "AddToCart" 
}, 
initialize: function(){ 
    this.model = Item; 
    this.collection = ItemCollection; 
    this.collection.bind("reset", this.render); 
}, 
render: function(){ 
    var data = { 
    items: ItemCollection.models 
    } 
    var compiledTemplate = _.template(ItemListTemplate , data); 
    $("#itemContainer").html(compiledTemplate); 
}, 
AddToCart:function(ev){ 
    ev.preventDefault(); 
    var id = $(ev.currentTarget).data("id"); 
    var item = ItemCollection.getByCid(id); 
    alert(item.get("ItemCode")); 
    var qty = 1; 
    var cartcollection = new CartCollection(); 
    cartcollection.add(item); 
    CartListView.render(); 
    var itemcode = cartcollection.where({ItemCode: item.get("ItemCode")}); 
    if(itemcode.length > 0){ alert("success"); } 
} 
}); 
return new ItemListView; 
}); 

cart_collection.js

define([ 
'underscore', 
'backbone', 
'model/cart_model' 
],function(_, Backbone, Cart){ 
var CartCollection = Backbone.Collection.extend({ 
    model: Cart, 
    initialize: function(){ 

    } 
}); 
return new CartCollection; 
}); 

cartlist_view.js

define([ 
'jquery', 
'underscore', 
'backbone', 
'model/cart_model', 
'collection/cart_collection', 
'text!templates/cart/cartlist.html' 
], function($, _, Backbone, Cart, CartCollection, CartListTemplate){ 

var Model = kendo.data.Model, 
    ObservableArray = kendo.data.ObservableArray; 

function wrapBackboneModel(backboneModel, fields) { 
    return Model.define({ 
     fields: fields, 
     init: function(model) { 
      if (!(model instanceof backboneModel)) { 
       model = new backboneModel(model); 
      } 

      Model.fn.init.call(this, model.toJSON()); 
      this.backbone = model; 
     }, 
     set: function(field, value) { 
      Model.fn.set.call(this, field, value); 

      this.backbone.set(field, value); 
     } 
    }); 
} 

function wrapBackboneCollection(model) { 
    return ObservableArray.extend({ 
     init: function(collection) { 
      ObservableArray.fn.init.call(this, collection.models, model); 

      this.collection = collection; 
     }, 

     splice: function(index, howMany) { 
      var itemsToInsert, removedItemx, idx, length; 

      itemsToInsert = Array.prototype.slice.call(arguments, 2); 

      removedItems = kendo.data.ObservableArray.fn.splice.apply(this, arguments); 

      if (removedItems.length) { 
       for (idx = 0, length = removedItems.length; idx < length; idx++) { 
        this.collection.remove(removedItems[idx].backbone); 
       } 
      } 

      if (itemsToInsert.length) { 
       for (idx = 0, length = itemsToInsert.length; idx < length; idx++) { 
        this.collection.unshift(itemsToInsert[idx].backbone); 
       } 
      } 

      return removedItems; 
     } 
    }); 
} 

kendobackboneCollection = wrapBackboneCollection; 
kendobackboneModel = wrapBackboneModel; 


var CartListView = Backbone.View.extend({ 
el: $("#cartContainer"), 

initialize: function(){ 
    this.collection = CartCollection; 
    this.model = Cart; 
    this.collection.bind("change", this.render); 
}, 
render: function(){ 
    console.log("here"); 
    this.el.html(CartListTemplate); 
    var CartWrapper = kendobackboneModel(Cart, { 
    ItemCode: { type: "string" }, 
    ItemDescription: { type: "string" }, 
    RetailPrice: { type: "string" }, 
    Qty: { type: "string" }, 
    }); 
    var CartCollectionWrapper = kendobackboneCollection(CartWrapper); 

    this.$("#grid").kendoGrid({ 
    editable: true, 
    toolbar: ["create"], 
    columns: ["ItemDescription", "Qty", "RetailPrice"], 
    dataSource: { 
     schema: {model: CartWrapper}, 
     data: new CartCollectionWrapper(CartCollection), 
    } 
    }); 

}, 

}); 
return new CartListView; 
}); 
+1

那麼究竟是什麼錯誤?它發生在哪裏? – tkone 2012-03-27 12:33:47

+0

你可以發佈你創建CartCollection構造函數的代碼嗎?這聽起來像是該代碼中的原因。在這段代碼運行之前,也許你沒有創建'CartCollection'? – joshuapoehls 2012-03-27 15:36:45

+0

@tkone錯誤是使用此代碼CartCollection.where();它說CartCollection不是一個函數。如果我將實例化它cartcollection = new CartCollection();然後使用cartcollection.where()它說CartCollection不是一個構造函數。 – jongbanaag 2012-03-28 01:14:16

回答

0

我認爲問題在於你是雙重實例化CartCollection。即,cart_collection.js返回new CartCollection(),並在itemlist_view.js中再次使用var cartcollection = new CartCollection();實例化它。

將該行更改爲var cartcollection = CartCollection;並查看它是如何工作的。顯然你也可以刪除cartcollection變量,並用CartCollection替換它的用法。

另一方面,我會認真考慮不要從cart_collection.js返回new CartCollection()。這感覺是一種非常糟糕的做法,因爲您只能使用該集合的一個實例。另外,開發人員不明白他們正在取回集合的實例,而不是構造函數。

我會建議返回CartCollection instaed,然後在itemlist_view.js文件中用new CartCollection()實例化它。這樣,如果需要,您將能夠稍後實例化更多這些集合。

相同推薦cartlist_view.js返回new CartListView()

+0

我標記你的答案是正確的,因爲我對模型有問題,你的回答是正確的關於我的模型問題。但對於集合,因爲我已經在我的cart_collection.js上返回新的CartCollection,所以我應該直接在我的視圖上使用CartCollection。但仍CartCollection.where({itemcode:item.get(「itemcode」)})問題仍然存在。它仍然說CartCollection.where不是一個函數。這會是一個錯誤嗎?因爲我可以添加但功能不起作用。 – jongbanaag 2012-03-29 02:06:06

+0

啊。它只是打擊了我的收藏沒有「where」功能。您可能想要改用'filter'函數。 – joshuapoehls 2012-03-29 03:17:00

+0

就是這樣。但基於文檔它有一個功能。謝啦。 ;) – jongbanaag 2012-03-29 03:21:57