[英]Backbone : view doesn't update (reset event)
我目前正在使用BackboneJS通過骨干模型從數據庫中獲取數據。
問題是我的應用程序沒有觸發我的重置事件,即使我的提取執行得很好,我的數據也沒有顯示在屏幕上。 如果我在控制台中執行app.messages.toJSON(),則會按需返回我的數據。
你有什么想法嗎? 這是我的代碼
var app = window.app = {};
// APP HERE
// Model
app.Message = Backbone.Model.extend({
url : 'messages'
});
app.message = new app.Message;
// Collection
app.Messages = Backbone.Collection.extend({
model : app.Message,
url : 'messages'
});
// Views
app.messages = new app.Message();
app.messages.fetch();
app.ListView = Backbone.View.extend({
template : Handlebars.compile($('#template-list').html()),
initialize : function(){
_.bindAll(this, 'render');
//this.collection.on('reset', this.render);
},
render : function(){
this.$el.html(this.template({
collection : this.collection.toJSON()
}));
return this;
}
});
我很傷心。
西蒙
編輯
在所有這些代碼之后
app.Router = Backbone.Router.extend({
routes: {
'*path' : 'home'
},
home: function(){
this.views = {};
this.views.list = new app.ListView({
collection : app.messages
});
$('#list-shell').empty().append(this.views.list.render().el);
}
});
app.router = new app.Router();
Backbone.history.start({pushState : true});
Backbone.emulateJSON = true;
app.messages = new app.Message();
我想這只是您在寫問題時犯的一個錯誤。
至於問題本身,如果您使用的是Backbone 1.0,則要觸發reset event
就必須使用reset flag
。
此外,如果您添加的代碼確實在第一個代碼之后 ,則您在創建Router 之前要獲取數據,因此要創建視圖。 即使獲取方法是異步的,您也無法控制此處的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.