我需要在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
}
});
现在让open的定义为:
function open(id) {
if (id) {
// do something
} else {
// do something else
}
}
我将从另一个函数调用open,并在调用它时传递id.所以根据我是否传递id,我需要做不同的事情.我如何在Backbone中执行此操作?
目前,通过点击调用id我希望它是未定义的.但是传递了一个事件对象.
为什么会发生这种情况?如何通过论证?
解决方法
解决此问题的另一种方法是使用一种完全不同的方法来处理点击,并调用“open”,以便其他进程可以调用它.正如另一个人所提到的,你在事件哈希中指定的方法是jquery委托包装器,因此对于params你可以做的事情并不多,因为你将得到的是委托提供的内容.因此,在这种情况下,创建另一个方法来执行将调用open的实际图标单击:
events: {
"click .icon": "open","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
}
}