다음과 같은 모드/수집 /보기 설정이 백본에 있습니다.백본 컬렉션에서 CID로 모델을 가져올 수 없습니다.
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
콜백 로깅 시도했지만 난 아직도 반환 정의되지 않은 얻을 ... 각 모델은 고유의 식별자를 가지고있다. 어떤 도움이라도 대단히 감사합니다. collection.get
가 인수로 모델 ID를 예상하기 때문에
'cid' 속성이 문제의 DOM 요소에 실제로 첨부되어 있는지 확인 했습니까? jsFiddle을 만들 수 있습니까? – Ben