Javascript 主干向视图传递事件并将其从父视图解除绑定
我想知道是否可以传递一个view en事件,然后在运行另一个方法之前再次停止该事件的运行 这是我的设置 主视图方法 子视图Javascript 主干向视图传递事件并将其从父视图解除绑定,javascript,backbone.js,backbone-views,event-delegation,event-binding,Javascript,Backbone.js,Backbone Views,Event Delegation,Event Binding,我想知道是否可以传递一个view en事件,然后在运行另一个方法之前再次停止该事件的运行 这是我的设置 主视图方法 子视图 在子视图渲染中,我想禁用传递的事件,然后在失败或成功时重新激活它。这是可能的吗?我将在这里做一个假设。我猜您真正想要了解的是,您不希望允许在一行中多次从父级触发notify.render,除非在其间调用了其中一个回调。基于这个假设,我不认为儿童观点应该是放弃倾听的观点。您需要暂时禁用父视图中调用changeAdmin的任何事件。我认为,把这一点放在孩子的角度来看,这两种观点
在子视图渲染中,我想禁用传递的事件,然后在失败或成功时重新激活它。这是可能的吗?我将在这里做一个假设。我猜您真正想要了解的是,您不希望允许在一行中多次从父级触发notify.render,除非在其间调用了其中一个回调。基于这个假设,我不认为儿童观点应该是放弃倾听的观点。您需要暂时禁用父视图中调用changeAdmin的任何事件。我认为,把这一点放在孩子的角度来看,这两种观点的结合比你打破SoC原则所需要的还要多
这样说,我会考虑在父视图中使用EG、THEY、UNCORTETAVER事件,然后在回调中重新启用事件,如:
changeAdmin: function(e) {
var self = this;
this.undelegateEvents();
var confirm_callback = function() {
// do confirm stuff
// re-enable events at the bottom of this callback
self.delegateEvents();
};
var notify = new App.Views.ConfirmationView({
confirm_callback: confirm_callback,
});
notify.render();
}
我删除了代码中与解释不相关的部分,但希望您能理解。对于fail_回调,您也会做类似的事情。我认为您应该重新考虑您的解决方案。你所解释的听起来非常不传统。与其解除事件绑定,不如设置某种标志。如果标志为false,则不要在事件处理程序中执行代码。我认为您应该在成功或失败后触发另一个事件,然后让子视图侦听该事件。您可能希望深入研究,然后不要在成功和失败中重写相同的代码
'use strict'
App.Views.ConfirmationView = Backbone.View.extend({
tagName: 'div',
className: 'notification alert alert-warning',
template: _.template( $('#tpl-cofirmation').html() ),
events: {
"click .js-confirm" : "runSuccessCallback",
"click .js-cancel": "runFailCallback"
},
initialize: function(options) {
this.options = options;
this.confirm = options.confirm_callback;
this.fail = options.fail_callback;
},
render: function() {
//var self = this;
this.$el.html(this.template({
message: this.options.message
})).css({
'z-index':'9999',
'position':'absolute',
'padding':'10px',
'left':'50%',
'top' : '0px',
'transform': 'translate(-50%, 0px)'
}).animate({
'top' : '150px'
}, 500, 'easeOutExpo').prependTo('body');
},
cancel: function() {
this.$el.hide();
},
confirm: function() {
this.$el.hide();
},
runSuccessCallback: function(){
this.confirm();
$.when(this.$el.animate({
'top' : '-150px'
}, 500, 'easeOutExpo').promise()).done(function(){
this.remove();
});
},
runFailCallback: function(){
this.fail();
$.when(this.$el.animate({
'top' : '-150px'
}, 500, 'easeOutExpo').promise()).done(function(){
this.remove();
});
}
});
changeAdmin: function(e) {
var self = this;
this.undelegateEvents();
var confirm_callback = function() {
// do confirm stuff
// re-enable events at the bottom of this callback
self.delegateEvents();
};
var notify = new App.Views.ConfirmationView({
confirm_callback: confirm_callback,
});
notify.render();
}