As an example, here's what I'm looking to do...
function doSomething(customParameter) {
console.log(customParameter);
}
var MyView = Backbone.View.extend({
// Other view stuff, skipping it...
events: {
'click .someClass': doSomething(customParameter)
}
});
The motivation here is I want to be able to pass variables (local to the view) to some other function outside the view. The reason I want to do this is so that the "other function" is only written once and not four times...it would seriously save my project over 100 lines of code.
After much googling I haven't found a solution to this...any ideas?
Thanks!
As an example, here's what I'm looking to do...
function doSomething(customParameter) {
console.log(customParameter);
}
var MyView = Backbone.View.extend({
// Other view stuff, skipping it...
events: {
'click .someClass': doSomething(customParameter)
}
});
The motivation here is I want to be able to pass variables (local to the view) to some other function outside the view. The reason I want to do this is so that the "other function" is only written once and not four times...it would seriously save my project over 100 lines of code.
After much googling I haven't found a solution to this...any ideas?
Thanks!
Share Improve this question asked Dec 11, 2013 at 19:30 MattMMattM 1,2891 gold badge14 silver badges25 bronze badges2 Answers
Reset to default 6Here is one way of doing it:
function doSomething(customParameter) {
console.log(customParameter);
}
var MyView = Backbone.View.extend({
// Other view stuff, skipping it...
events: {
'click .someClass': function(ev) { doSomething(this.customParameter); }
}
});
In this example, you declare a function that passes the arguments needed from the local view to that doSomething
method.
Another way could be to call the doSomething() with the context of the current view, if you don't want to pass all the required arguments:
function doSomething(ev) {
console.log(this.customParameter); // here, the context this will be the view.
}
var MyView = Backbone.View.extend({
// Other view stuff, skipping it...
events: {
'click .someClass': function(ev) { doSomething.call(this, ev); }
}
});
A more OOP approach would be to have a base View for that mon code:
var MyBaseView = Backbone.View.extend({
doSomething: function(ev) {
console.log(this.customParameter);
}
});
var MyView = MyBaseView.extend({
extends: {
'click .someClass': 'doSomething'
}
});
Probably there are more ways of doing it, you just have to see which scenario applies best to your needs.
You can try calling a method that is internal to the view which calls the method in question
var MyView = Backbone.View.extend({
// Other view stuff, skipping it...
events: {
'click .someClass': 'callSomething'
},
callSomething: function() {
doSomething(customParameter);
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745106137a4611565.html
评论列表(0条)