简体   繁体   English

如何将参数传递给backbone.js中events对象中绑定的函数

[英]How to pass arguments to functions binded in events object in backbone.js

I need to have arguments to the functions used in the events object in Backbone. 我需要在Backbone中的events对象中使用函数的参数。

var DocumentRow = Backbone.View.extend({

    tagName: "li",

    className: "document-row",

    events: {
        "click .icon": "open",
        "click .button.edit": "openEditDialog",
        "click .button.delete": "destroy"
    },

    render: function () {
        // do something
    }
});

Now let the definition of open be: 现在让open的定义为:

function open(id) {
    if (id) {
      // do something
    } else {
      // do something else
    }
}

I will call open from another function and will pass id when I call it. 我将从另一个函数调用open,并在调用它时传递id。 So based on whether I pass id or not I need to do different things. 所以根据我是否传递id,我需要做不同的事情。 How do I do this in Backbone? 我如何在Backbone中执行此操作? Currently, id when called via click I expect it to be undefined. 目前,通过点击调用id我希望它是未定义的。 But an event object is passed. 但是传递了一个事件对象。

Why does this happend and how can I pass an argument? 为什么会发生这种情况?如何通过论证?

Another way to approach this is to use a completely different method that handles the click, and calls "open" so it can be called by another process. 解决这个问题的另一种方法是使用一种完全不同的方法来处理点击,并调用“打开”,这样它就可以被另一个进程调用。 As another person mentioned, methods you specify in the events hash are jquery delegate wrappers, so there's not much you can do with respect to params as what you'll get is what delegate provides. 正如另一个人所提到的,你在事件哈希中指定的方法是jquery委托包装器,因此对于params你可以做的事情并不多,因为你将得到的是委托提供的内容。 So in that case, create another method that does the actual icon click that will invoke open: 因此,在这种情况下,创建另一个方法来执行将调用open的实际图标单击:

events: {
    "click .icon": "open",
    "click .button.edit": "openEditDialog",
    "click .button.delete": "destroy"
},
/**
* Specifically handles the click and invokes open
*/
handleIconClick : function(event) {
    // ...process 'event' and create params here...
    this.open(params);
},
/**
* This can be called remotely
*/
open : function(id) {
    if (id) {
      // do something
    } else {
      // do something else
    }
}

you can see here: http://backbonejs.org/docs/backbone.html#section-156 你可以在这里看到: http//backbonejs.org/docs/backbone.html#section-156

The most critical sentence is: 最关键的句子是:

this.$el.delegate(selector, eventName, method);

because backbone's events is jquery's delegate( http://api.jquery.com/delegate/ ),so the parameter is passed just is jquery's delegate default parameters passed. 因为backbone的事件是jquery的委托( http://api.jquery.com/delegate/ ),所以传递的参数只是jquery的委托默认参数传递。

you can update your code like this: 你可以这样更新你的代码:

......

open : function(event) {
    //get id from event 
    var id = event.xxx ;//pseudo code
    if (id) {
      this.openid(id);
    } else {
      // do something else
    }
},
//your another function direct call openid
openid: function(id){
    // do something
}
......

如果您知道ID变量的类型,您可以使用javascript typeof()来检查是否为true,或者您可以使用来自UnderscoreJS的_.isObject检查ID是否为对象

I think Brendan or fejustin's answers are the best (actually I voted for them). 我认为Brendan或者fejustin的答案是最好的(实际上我投票给他们)。 But if you're really committed to having just one function, you could make id the second parameter and ignore the first one. 但是如果你真的致力于只有一个函数,你可以将id作为第二个参数并忽略第一个参数。

open : function(evt, id){
  if(id){
     //whatever
  } else {

  }
},

someOtherMethod : function(){
  this.open(null, id);
}

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

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