主干视图在辅助提取时未更新
Backbone View not updating on secondary fetch
我正在使用Backbone来显示项目列表,由服务器上的JSON填充。
//Models
var Note = Backbone.Model.extend();
var NoteList = Backbone.Collection.extend({
model: Note,
url: "http://www.myserver.com/notes/notes.json"
});
var Notes = new NoteList;
//Views
var NoteView = Backbone.View.extend({
tagName: "li",
template: _.template($('#noteTemplate').html()),
initialize: function() {
this.listenTo(this.model, 'change', this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var AppView = Backbone.View.extend({
el: $("#specificationNotes"),
noteTemplate: _.template($('#noteTemplate').html()),
initialize: function() {
this.listenTo(Notes, 'reset', this.addAll);
this.listenTo(Notes, 'all', this.render);
Notes.fetch({reset:true});
},
addOne: function(note) {
var view = new NoteView({model: note});
this.$("#notesList").append(view.render().el);
},
addAll: function() {
Notes.each(this.addOne, this);
}
});
var App = new AppView;
初始提取会正确抓取内容并显示列表。 我正在使用jQuery对话框来"添加注释",该对话框只是简单地将POST发布到RESTful API,在ajax帖子成功后,我调用
Notes.fetch();
并且模型已更新,但视图不会更改。
您的 AppView 没有呈现方法,因此第一次提取会导致绘制,因为处理程序绑定到"重置"事件,但第二次提取没有。这是一个蛮力:
render: function() {
this.$el.html(""); // so we don't readd the same items on re-render
this.addAll();
}
效率低下,因为它每次都会从头开始重新绘制所有笔记,但它至少应该让你在屏幕上看到实时更改。也许更换
this.listenTo(Notes, 'reset', this.addAll);
this.listenTo(Notes, 'all', this.render);
跟
this.listenTo(Notes, 'reset', this.render);
this.listenTo(Notes, 'sync', this.render);
相关文章:
- Javascript循环不会自我更新
- 添加文字和评论功能更新Div
- AngularJS:ng之后,重复$scope值未按预期更新
- 如何通过数组更新角度子范围
- Ajax聊天消息重复而不仅仅是更新
- 通过CSV文件上载更新数据库表
- 平均值:无法将数据更新到数据库
- $rootScope未使用forEach进行更新
- d3基于用户选择动态更新节点
- 有条件更新d3.js力图中节点的最佳方法
- 在数据提取完成之前进行页面渲染
- Angular:更新一次性绑定的数据
- 主干.js更改模型的 url 参数,提取不会更新获取的数据
- 主干视图在辅助提取时未更新
- 主干提取是添加新行以查看,而不是更新现有行
- 如何从使用ajax更新的json文件中提取值,并将其用作php变量
- 如何从MongoDB中提取项目,并在输入新项目时进行更新
- 为什么不't Settimeout提取更新的图像URL
- 美元.getJSON没有提取更新的信息
- 异步数据提取后更新jquery.sparkline