是否可以为 listenTo 回调绑定函数参数?
到目前为止,我已经添加了一个包装方法“myHandler”,我想摆脱它:
// Basic marionette layout
var view = Marionette.Layout.extend({
initialize: function() {
// wrapping view logic inside a custom object
this.controller = new MyViewController();
},
// creates a sub view and adds event handlers
someFunc: function() {
var subView = new MySubView();
// HERE: how to bind args for callback?
this.listenTo(subView, "myEvent", this.myHandler, this);
},
// this is a dummy wrapper that I want to remove
myHandler: function(e) {
this.controller.handleIt(this, e);
},
我想做的是:
someFunc: function() {
var subView = new MySubView();
// here wrapIt binds 'this' as first argument for handleIt
this.listenTo(subView, "myEvent",
wrapIt(this.controller.handleIt, this), this);
}