简体   繁体   English

Backbone:如何绑定listenTo-callback的参数?

[英]Backbone: How to bind arguments for listenTo-callback?

Is it possible to bind function arguments for listenTo callback? 是否可以为listenTo回调绑定函数参数?

I've till now added a wrapper methods 'myHandler' which I would like to get rid of: 我到现在为止添加了一个包装方法'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);
},

What I want to do is something like: 我想做的是:

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);
}

listenTo is accepting only 3 arguments. listenTo只接受3个参数。 If you need to bind function to some object then cross-browser way to do this is using underscore _.bind function: 如果需要将函数绑定到某个对象,那么跨浏览器的方式是使用下划线_.bind函数:

this.listenTo(subView, "myEvent", _.bind(this.myHandler, this))

However it's mostly not needed as object you are calling listenTo on is the default context. 但是,大多数情况下不需要将调用listenTo对象作为默认上下文。 To read more see these github issues: 要阅读更多信息,请参阅以下github问题:

Why not just use a function in your listenTo function call? 为什么不在listenTo函数调用中使用函数? Like so: 像这样:

// 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();

    this.listenTo(subView, "myEvent", function (e) {
      this.controller.handleIt(this, e);
    }, this);
  }, 

Underscore is a hard dependency for Backbone which means you can use _.bind to set the context: Underscore是Backbone的硬依赖,这意味着您可以使用_.bind来设置上下文:

bind _.bind(function, object, [*arguments]) bind _.bind(function,object,[* arguments])
Bind a function to an object, meaning that whenever the function is called, the value of this will be the object. 将函数绑定到对象,这意味着无论何时调用函数,它的值都将是对象。 Optionally, pass arguments to the function to pre-fill them, also known as partial application. (可选)将参数传递给函数以预填充它们,也称为部分应用程序。

Your example could be written as 你的例子可以写成

someFunc: function() {
    var subView = new MySubView(),
        callback = _.bind(this.controller.handleIt, this);

    this.listenTo(subView, "myEvent", callback, this);
}

If you want the context as the first argument to your function, add it as a third argument to _.bind 如果您希望将上下文作为函数的第一个参数,请将其作为第三个参数添加到_.bind

someFunc: function() {
    var subView = new MySubView(),
        callback = _.bind(this.controller.handleIt, this, this);

    this.listenTo(subView, "myEvent", callback, this);
}

是的,您可以使用JQuery $ .proxy中的代理函数(this.controller.handler,this)来执行此操作。请参阅此处的文档http://api.jquery.com/jQuery.proxy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM