我在骨幹中有以下模式/集合/視圖設置。無法通過CID獲取Backbone集合中的模型
var Card = {};
//card model
Card.Model = Backbone.Model.extend();
//collection
Card.Collection = Backbone.Collection.extend({
url: '/api/cards',
model: Card.Model
});
//column view
Card.MainView = Backbone.View.extend({
className: 'column',
append: function(model) {
var view = Card.View.extend({
model : model,
attributes: { cid : model.cid }
});
var v = new view();
this.$el.append(v.el);
},
initialize: function() {
var self = this;
_.bindAll(this, "sortable");
this.collection.fetch({
success: function() {
self.render();
}
});
},
render: function() {
var self = this;
for(var i = 0; i < this.columns.length; i++) {
var col = this.columns[i];
var column = Card.ColumnView.extend({
data: col,
cards: self.collection.where({ position : col.position })
});
var col = new column();
this.$el.append(col.el);
}
this.sortable();
},
sortable: function() {
var self = this;
$(this.el).find('.card-container').sortable({
connectWith: '.card-container',
receive: _.bind(function(event, ui) {
var cid = $(ui.item).attr('cid');
var card = self.collection.get(cid);
//console.log(self.collection.last());
console.log('cid', self.collection.get(cid));
console.log('index 1', self.collection.at(1));
console.log('last', self.collection.last());
console.log('collection', self.collection);
}, this)
});
}
});
當我試圖讓從的MainView的收集模式在sortable
功能也不會記錄由CID模型。我得到以下輸出。
cid undefined CardMetaCollection.js:97
index 1 d {collection: d, attributes: Object, _escapedAttributes: Object, cid: "c2", changed: Object…} CardMetaCollection.js:98
last d {collection: d, attributes: Object, _escapedAttributes: Object, cid: "c249", changed: Object…} CardMetaCollection.js:99
collection d {length: 249, models: Array[249], _byId: Object, _byCid: Object, url: "/api/cards"…}
我試圖記錄在success
回調的獲取方法,但我還是得到了一個未定義返回......每個模型也有其自己的CID。任何幫助深表感謝。
您是否確保'cid'屬性實際上被附加到相關的DOM元素上?你可以做一個jsFiddle嗎? – Ben 2013-03-06 17:08:57