我在主干中有以下模式/集合/视图设置。
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)
});
}
});
当我尝试从sortable
函数中的 MainView 集合中获取模型时,它不会按 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
fetch 方法的回调,但我仍然得到未定义的返回......每个模型都有自己的 cid。任何帮助深表感谢。