Backbone.js 集合创建函数触发添加事件太快
(使用主干网0.9.10) 我有一个排序列表视图,用户可以单击按钮来显示模式视图。列表视图有一个计数器,显示列表中包含的项目数量。单击模式视图中的按钮,对传递到两个视图中的集合执行Backbone.js 集合创建函数触发添加事件太快,backbone.js,Backbone.js,(使用主干网0.9.10) 我有一个排序列表视图,用户可以单击按钮来显示模式视图。列表视图有一个计数器,显示列表中包含的项目数量。单击模式视图中的按钮,对传递到两个视图中的集合执行create。这将在列表视图中触发add事件,该事件依次运行此函数: renderCount: function () { // Container for model count var $num = this.$el.find('.num'); if ($num.length > 0
create
。这将在列表视图中触发add
事件,该事件依次运行此函数:
renderCount: function () {
// Container for model count
var $num = this.$el.find('.num');
if ($num.length > 0) {
// 'count' is a value returned from a service that
// always contains the total amount of models in the
// collection. This is necessary as I use a form of
// pagination. It's essentially the same as
// this.collection.length had it returned all models
// at once.
$num.html(this.collection.count);
}
}
然而,add
似乎会在集合有机会更新模型计数之前立即启动(根据文档,这是应该的)。我查看了sync
,但它似乎什么都没做
如何确保在调用renderCount
之前更新集合
以下是列表视图initialize
函数,供参考:
initialize: function (options) {
this.collection = options.collection;
this.listenTo(this.collection, 'add remove reset', this.renderCount);
this.render();
}
编辑:
原来我忘了在模式视图中的success
上重新绘制集合
$num.html(this.collection.count);
应该是:
$num.html(this.collection.size());
主干。集合使用从下划线导入的方法,下面是列表:结果发现我忘记在模式视图中的
success
上重新提取集合。如何更新count
属性?有AJAX请求吗?@Nikosh谢谢。这暗示了我忘记了什么,即在创建成功时更新集合的获取。count
是集合中的自定义变量。在这方面,其目的不尽相同。
$num.html(this.collection.count);