バックボーンに次のモード/コレクション/ビューのセットアップがあります。
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"…}
fetch メソッドのコールバックにログインしようとしましたsuccess
が、まだ undefined が返されます... 各モデルには独自の cid があります。どんな助けでも大歓迎です。