Tôi đang cố gắng lặp qua các mô hình được tìm nạp theo bộ sưu tập.collection.each() không lặp qua các mô hình
Tôi đã folowing đoạn mã:
initialize: function() {
this.collection = new UserCollection();
this.collection.fetch();
this.render();
},
renderCollection: function() {
console.log("rendering collection");
this.collection.each(function(index,model){
console.log("model");
});
console.log(this.collection);
},
render: function() {
this.template = _.template(template, {});
this.$el.html(this.template);
// some other stuff
this.renderCollection();
}
và kết quả:
rendering collection
d {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…}
_byId: Object
_idAttr: "id"
length: 4
models: Array[4]
0: d
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
created: "2013-02-13 09:22:42"
id: "1"
modified: "2013-02-13 09:22:42"
role: "admin"
username: "[email protected]"
__proto__: Object
changed: Object
cid: "c5"
collection: d
id: "1"
__proto__: e
1: d
2: d
3: d
length: 4
__proto__: Array[0]
__proto__: e
user_list.js:25
Vì vậy, các phương pháp lấy làm việc - ở bãi đối tượng tôi có thể tìm thấy 4 hồ sơ lấy nhưng lặp trong bộ sưu tập không không hoạt động ...
sự cố có thể đến từ đầu ra máy chủ cần phải là một mảng đối tượng json hợp lệ. tức là trong php một mảng json_encoded được lập chỉ mục –
Trình lặp của bạn phải là: 'this.collection.each (function (model) {...});' HOẶC 'this.collection.each (chức năng (mô hình, chỉ mục) {...});' OP có chỉ mục và mô hình sai đường xung quanh. –
Tôi đã phải thực hiện từng bộ sưu tập như vậy (không phải bộ sưu tập): http://stackoverflow.com/questions/11726943/for-loop-over-backbone-collection...đây là vấn đề của tôi. – Kelly