admin管理员组文章数量:1244265
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
}
}
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.
Why does this happend and how can I pass an argument?
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
}
}
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.
Why does this happend and how can I pass an argument?
Share Improve this question edited Aug 1, 2012 at 16:43 Radu 8,6998 gold badges56 silver badges93 bronze badges asked Aug 1, 2012 at 15:19 Gautham RenganathanGautham Renganathan 6451 gold badge9 silver badges18 bronze badges 1- This is related -- stackoverflow./questions/5680807/… – storm_m2138 Commented Dec 2, 2013 at 20:04
4 Answers
Reset to default 5Another way to approach this is to use a pletely 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 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/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./delegate/ ),so the parameter is passed just is jquery's delegate default parameters passed.
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
}
......
If you know the type of the ID variable you can use javascript typeof()
to check if true or you can check if ID is object with _.isObject from UnderscoreJS
I think Brendan or fejustin's answers are the best (actually I voted for them). But if you're really mitted to having just one function, you could make id
the second parameter and ignore the first one.
open : function(evt, id){
if(id){
//whatever
} else {
}
},
someOtherMethod : function(){
this.open(null, id);
}
本文标签: javascriptHow to pass arguments to functions binded in events object in backbonejsStack Overflow
版权声明:本文标题:javascript - How to pass arguments to functions binded in events object in backbone.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740197667a2239622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论