1
我在查看为什么模型没有出现在收藏视图实例化时遇到问题。数组被调用,当我打电话给notes.models
时,我得到正确的记录数。但是,console.log(note)
应该被调用,没有任何反应。所以,抓取似乎正在工作,并有数据,我可以在响应中看到它,它只是没有被传递到collection.each
视图。欢迎任何帮助。这是一切。调用集合时,模型没有填充数据。 Backbone和Django-Tastypie
TastpieCollection和TasttypieModel来自http://paltman.com/2012/04/30/integration-backbonejs-tastypie/这似乎工作得很好。
$(function() {
// Note: The model and collection are extended from TastypieModel and TastypieCollection
// to handle the parsing and URLs
window.Note = TastypieModel.extend({});
window.Notes = TastypieCollection.extend({
model: Note,
url: NOTES_API_URL
});
// starts by assigning the collection to a variable so that it can load the collection
window.notes = new Notes();
window.NoteView = Backbone.View.extend({
className: "panel panel-default note",
template: _.template($('#notes-item').html()),
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
window.NoteListView = Backbone.View.extend({
id: "notes-block",
className: "panel panel-info",
template: _.template($('#notes-item-list').html()),
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('change', this.render);
},
render: function() {
this.$el.html(this.template());
this.collection.each(function(note){
//var view = new NoteView({ model: note });
console.log(note);
//$('#notes-list').append(view.render().el);
});
console.log('done');
return this;
}
});
window.NotesRouter = Backbone.Router.extend({
routes: {
"": "list",
'blank': 'blank'
},
initialize: function() {
this.notesView = new NoteListView({
collection: window.notes
});
notes.fetch();
},
list: function() {
$('#app').empty();
$('#app').append(this.notesView.render().el);
console.log(notes.length);
},
blank: function() {
$('#app').empty();
$('#app').text('Another view');
}
});
window.notesRouter = new NotesRouter();
Backbone.history.start();
})
在尝试使用'this.collection.each'之前,确定'fetch'(这是一个AJAX调用)从服务器返回吗? –
我很确定。我将notes.fetch移到了一堆不同的位置。不用找了。只是为了检查,我删除了抓取,所以它不会调用任何东西,并且notes.models中没有任何东西。所以抓取工作正常。 –
尝试使用'console.log(this.collection.toJSON())','console.log'将实时引用放入控制台中,因此当您调用console.log时,您在控制台中看到的内容不一定就是那里的内容。 –