Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干:如何绑定listenTo回调的参数?_Javascript_Backbone.js_Marionette - Fatal编程技术网

Javascript 主干:如何绑定listenTo回调的参数?

Javascript 主干:如何绑定listenTo回调的参数?,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,是否可以为listenTo回调绑定函数参数 到目前为止,我已经添加了一个包装方法“myHandler”,我想去掉它: // Basic marionette layout var view = Marionette.Layout.extend({ initialize: function() { // wrapping view logic inside a custom object this.controller = new MyViewController(); },

是否可以为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);
}

下划线是主干的硬依赖项,这意味着您可以使用它设置上下文:

bind\uuz.bind(函数、对象、[*参数])
将函数绑定到对象,这意味着每当调用函数时 这将是对象。(可选)将参数传递给函数以 预填充,也称为部分应用

你的例子可以写成

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

    this.listenTo(subView, "myEvent", callback, this);
}
如果要将上下文作为函数的第一个参数,请将其作为第三个参数添加到
.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) 请参阅此处的文档

listenTo
只接受3个参数。如果您需要将函数绑定到某个对象,则跨浏览器执行此操作的方法是使用下划线
函数:

this.listenTo(subView, "myEvent", _.bind(this.myHandler, this))
但是,它基本上不需要,因为您正在调用的
listenTo
对象是默认上下文。要了解更多信息,请参阅以下github问题:


    • 为什么不在listenTo函数调用中使用函数呢?像这样:

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

      这个.controller.handleIt是什么?@Qantas94Heavy这是一个用于包装视图逻辑的FSM:这些答案有帮助吗?这是一个过时的解决方案,listenTo方法的第四个参数已从主干核心中删除:您可以改为使用u.bind()