Skip to content

Instantly share code, notes, and snippets.

@majuric
Last active August 29, 2015 14:01
Show Gist options
  • Save majuric/e9695ecb1fe3761707c9 to your computer and use it in GitHub Desktop.
Save majuric/e9695ecb1fe3761707c9 to your computer and use it in GitHub Desktop.
Call a method in Backbone.js with a string as a parameter
var View = Backbone.View.extend({
callFunc : function(funcName) {
// Use array notation to fetch the function definition and call it...
this[funcName]();
},
foo : function() {
console.log('called foo');
},
bar : function() {
console.log('called bar');
}
});
var view = new View();
view.callFunc('foo');
>> called foo
view.callFunc('bar');
>> called bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment