如何将参数传递给在事件绑定功能Backbone.js的对象 [英] How to pass arguments to functions binded in events object in backbone.js

查看:761
本文介绍了如何将参数传递给在事件绑定功能Backbone.js的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有参数的事件中使用的函数对象骨干

I need to have arguments to the functions used in the events object in Backbone.

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:

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

我会从另一个调用函数开放,将通过ID,当我把它。因此,基于我是否传递ID或没有,我需要做不同的事情。如何在骨干做到这一点?
目前,ID的时候通过点击我希望它是不确定的调用。但是,一个事件对象传递。

I will call open from another function and will pass id when I call it. So based on whether I pass id or not I need to do different things. How do I do this in Backbone? Currently, id when called via click I expect it to be undefined. But an event object is passed.

为什么这happend我怎么能传递参数?

Why does this happend and how can I pass an argument?

推荐答案

要解决这个另一种方法是使用处理一下,并呼吁开放,因此它可以被其他进程称为一个完全不同的方法。正如其他人所提到的,您在事件哈希指定方法是jQuery的委托包装,这样你会得到什么代表提供了有关有没有什么可以做,以PARAMS。因此,在这种情况下,创建另一个执行实际的图标点击将调用open方法:

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. So in that case, create another method that does the actual icon click that will invoke 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...
    this.open(params);
},
/**
* This can be called remotely
*/
open : function(id) {
    if (id) {
      // do something
    } else {
      // do something else
    }
}

这篇关于如何将参数传递给在事件绑定功能Backbone.js的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆