Javascript backbone.js在事件(vent)完成时做些什么?
我有一个主视图,我希望在以下情况下做一些更改(和/或):Javascript backbone.js在事件(vent)完成时做些什么?,javascript,jquery,events,backbone.js,Javascript,Jquery,Events,Backbone.js,我有一个主视图,我希望在以下情况下做一些更改(和/或): 该事件由“我的所有盒子”处理 我所有的盒子都做了一些动画 不能让我的头围绕着这个(漫长的工作日)。。。 请帮助=) 更好的做法是什么 我在找这样的东西: $('button').click(function(){ App.vent.trigger("box:change"); }); App.BoxView = Backbone.View.extend({ initialize: function(options) {
$('button').click(function(){
App.vent.trigger("box:change");
});
App.BoxView = Backbone.View.extend({
initialize: function(options) {
this.listenTo( App.vent, "box:change", this.alter ); // animate, etc.
},
...
});
更新:
如果我的应用程序中有一长串事件-完成-事件-完成(而不是循环),那么设置此队列的更好方法是什么呢?使用jquery,我找到了promise,只需要弄清楚如何将其应用于我的所有视图 (更新) 现在,我已经这样做了。。。
我一直在寻找解决方案,发现了这个主干插件,它在主干中添加了一个“triggerThen”方法。事件和其他主干对象,允许您触发一个事件,然后在所有事件侦听器完成后调用一个函数
如果有一个官方的主干网解决方案,尽管它在整个主干网.Events框架中使用了承诺,那就太好了。您的
App.vent.trigger()
调用是单线程的,因此当trigger
返回时,所有处理程序都将返回。动画是所有工作的地方。@muistooshort是的,我可以以某种方式收集动画,并使用promise设置所有动画完成时的回调。但这也意味着我的alter()中只需要动画(或设置超时)……你知道有多少个框吗?你可以让每个框在动画结束时触发某种形式的“我做了我正在做的一切”事件。然后,如果您知道在调用触发器时有多少个框(n
),您可以列出这些自定义事件,并在收到n
事件时执行需要执行的操作。@muistooshort这就是我现在正在做的,我对这种方法不太满意,所以我寻找一种不同的方法……嗯,有人必须知道有多少动画正在运行,否则就无法知道它们都完成了。
App.MainView = Backbone.View.extend({
initialize: function(options) {
// when all the events are complete
this.listenTo( App.vent, "box:change" ).onComplete( this.rearrange_stuff );
// when all the animations are complete
this.listenTo( App.animationBuffer, "box:fadeOut" ).onComplete( this.rearrange_stuff );
},
...
});
App.vent = _.extend(Backbone.Events, {
promise: function(name){
if(typeof this._bills[name] === "undefined"){
this._bills[name] = 0;
} else {
this._bills[name] ++;
}
},
keepPromise: function(name){
var that = this;
setTimeout( function(){
if(typeof that._checks[name] === "undefined"){
that._checks[name] = 0;
} else {
that._checks[name] ++;
}
if(typeof that._bills[name] !== "undefined"){
if( that._checks[name] >= that._bills[name] ){
that._checks[name] = 0; // reset
that._bills[name] = 0; // reset
that.trigger(name); // emit event
}
}
}, 30);
},
_bills: {},
_checks: {}
});
// usage:
alter: function() {
App.vent.promise('some:event');
// Animate, or not..
App.vent.pay('some:event');
},