在收集主干复位事件 [英] Backbone reset event in collection
本文介绍了在收集主干复位事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何骨干复位事件的作品?
据我了解
How does Backbone reset event works? As far as I understand
- 从集合中删除所有机型
- 添加新的牵强模式,以收集
- 火灾复位事件
在我的情况下,每个模型借鉴SVG的东西,所以我应该叫从集合中移除模型之前删除的功能。当模型从集合中删除的事件被触发?
In my case each model draw something on SVG so I should call remove function before removing model from collection. Which event is triggered when model is removed from collection?
推荐答案
作为@保罗指出,有前重置
解雇没有predefined事件。但是,您可以通过覆盖重置
方法在您的收藏提供你自己的。例如,
As @Paul noted, there is no predefined event fired before a reset
. However, you can provide your own by overriding the reset
method on your collection. For example,
var SVGCollection = Backbone.Collection.extend({
reset: function(models, options) {
options = options || {};
if (!options.silent) {
this.trigger('prereset', this, options);
}
Backbone.Collection.prototype.reset.call(this, models, options);
}
});
和示例用法
var c = new SVGCollection([
{id: 1},
{id: 2}
]);
c.on('prereset', function() {
console.log(c.pluck('id'));
});
c.on('reset', function() {
console.log(c.pluck('id'));
});
c.reset({id: 3});
请参阅 http://jsfiddle.net/nikoshr/8vV7Y/ 以演示
您还可能引发对每个型号的事件。
You could also trigger events on each model.
这篇关于在收集主干复位事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文