我有問題,collection.get和model.get返回undefined。Collection.get在backbone.js中返回undefined
這裏是我的initializecode
initialize: function() {
this.collection = new productsCollection();
this.model = new productModel();
}
這裏是我的渲染代碼
this.collection.fetch({
success: function (product) {
console.log(product);
$(that.el).html(_.template(productListTemplate, { products: product.models, _: _ }));
}
});
我的產品列表中顯示的罰款。當我在每個產品上點擊我得到其中名稱可以更改
我想要得到的模型,並觸發保存
設定新的名字,但我無法得到該產品的模型這裏是一個彈出代碼
$("#productName").val($(e.currentTarget).html());
var ID = $(e.currentTarget).data("id");
var item = this.collection.get(ID);
console.log("start..........");
console.log(item);
console.log(ID)
// console.log(this.collection);
console.log(this.model.get(item));
console.log("end..........");
$('.modal').modal('toggle');
我能夠得到正確的ID在控制檯,但沒有收藏和模型
一些可以幫助一個由於事先
UPDATE 這裏是完整的視圖代碼
function ($, _, Backbone, popupModal, productTab, productsCollection, productListTemplate, productModel) {
var productListView = Backbone.View.extend({
el: $("#page"),
initialize: function() {
this.collection = new productsCollection();
this.model = new productModel();
this.model.bind('change', this.loadResults, this);
},
render: function() {
this.loadResults();
},
loadResults: function() {
var that = this;
this.collection.fetch({
success: function (product) {
console.log(product);
$(that.el).html(_.template(productListTemplate, { products: product.models, _: _ }));
}
});
var modalWindow = $(".modal").modal({
show: false,
backdrop: true,
closeOnEscape: true
});
$('#createProduct').click(function (e) {
this.modalWindow.modal('show');
});
},
// This will simply listen for scroll events on the current el
events: {
"click #saveProduct": "saveProduct",
"click .productTabs": "productTabs",
"click .productDetails": "productDetails"
},
saveProduct: function() {
this.model.set({
Name: $('#productName').val()
});
this.model.save({ id: this.model.get('id') },
{
success: function (model, response) {
// console.log("success");
},
error: function (model, response) {
// console.log(response);
var errorMsg = JSON.parse(response.responseText);
$(".errorMessage").html('<div class="alert alert-error">' + errorMsg.Error + '</div>');
}
});
},
productTabs: function (e) {
e.preventDefault();
$(this).tab('show');
},
productDetails: function (e) {
e.preventDefault();
$("#productName").val($(e.currentTarget).html());
var ID = $(e.currentTarget).data("id");
var item = this.collection.get(ID);
console.log("start..........");
console.log(item);
console.log(ID)
// console.log(this.collection);
console.log(this.collection.models.get(item));
console.log("end..........");
$('.modal').modal('toggle');
}
});
return new productListView;
});
UPDATE反應
this.collection
b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function() { [native code] }
_removeReference: function() { [native code] }
length: 2
models: Array[2]
0: b.hasOwnProperty.e
1: b.hasOwnProperty.e
length: 2
__proto__: Array[0]
__proto__: s
它有2種型號,我的清單上還有2產品
this.model
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
ID: ""
Name: ""
hRef: ""
__proto__: Object
cid: "c2"
__proto__: s
屬性是空
這給了我以下
cid: "view1"
collection: b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function() { [native code] }
_removeReference: function() { [native code] }
length: 2
models: Array[2]
__proto__: s
model: b.hasOwnProperty.e
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
cid: "c2"
__proto__: s
options: Object
__proto__: s
UPDATE這是我看到的時候我擴大我的收藏
b.hasOwnProperty.e
_byCid: Object
_byId: Object
_onModelEvent: function() { [native code] }
_removeReference: function() { [native code] }
length: 2
models: Array[2]
0: b.hasOwnProperty.e
_callbacks: Object
_changed: false
_changing: false
_escapedAttributes: Object
_previousAttributes: Object
attributes: Object
ID: "7e0c94fc-7c16-45c9-84a9-a0690103b946"
Name: "dsa"
hRef: "Product/dsa"
__proto__: Object
cid: "c3"
collection: b.hasOwnProperty.e
__proto__: s
1: b.hasOwnProperty.e
length: 2
__proto__: Array[0]
__proto__: s
這裏的代碼示例非常_dismembered_,我們不能看到一個部分與其他合作如何。請提供更一致和完整的示例代碼。 – fguillen
@fguillen我發佈了整個代碼 – Kishore
當你運行註釋行'console.log(this.collection);'時,你到底看到了什麼?你也看到你是否會運行'console.log(this.model);'? ..甚至'console.log(this);'? – fguillen