If I have a method name in a string, in Ruby I can use send
to dynamically dispatch methods, e.g.
method_name = "delete"
send method_name
I can take advantage of interpolation too:
method_name = "add"
send "#{method_name}_task", args
I've 2 functions defined in javascript, one to delete, one to update something. A button for each is dynamically added and, at the moment, just the delete method gets bound via button.on "click"
, e.g.
b.on "click", (event) ->
event.preventDefault() # stop submission via postback
this_button = $(this)
task_id = this_button.data("task-id")
delete_task( task_id, this_button )
false
I'd like to be able to do this:
method_name = "delete"
b.on "click", (event) ->
event.preventDefault() # stop submission via postback
this_button = $(this)
task_id = this_button.data("task-id")
send "#{method_name}_task", task_id, this_button
false
The only difference between the binding of the 2 functions is this one line. If there's an obvious way, it'd be helpful to cut down on the repetition. I haven't found anything in my searches, so if anyone could help it would be much appreciated.
If I have a method name in a string, in Ruby I can use send
to dynamically dispatch methods, e.g.
method_name = "delete"
send method_name
I can take advantage of interpolation too:
method_name = "add"
send "#{method_name}_task", args
I've 2 functions defined in javascript, one to delete, one to update something. A button for each is dynamically added and, at the moment, just the delete method gets bound via button.on "click"
, e.g.
b.on "click", (event) ->
event.preventDefault() # stop submission via postback
this_button = $(this)
task_id = this_button.data("task-id")
delete_task( task_id, this_button )
false
I'd like to be able to do this:
method_name = "delete"
b.on "click", (event) ->
event.preventDefault() # stop submission via postback
this_button = $(this)
task_id = this_button.data("task-id")
send "#{method_name}_task", task_id, this_button
false
The only difference between the binding of the 2 functions is this one line. If there's an obvious way, it'd be helpful to cut down on the repetition. I haven't found anything in my searches, so if anyone could help it would be much appreciated.
Share Improve this question edited Jul 23, 2013 at 8:31 ian asked Jul 23, 2013 at 8:20 ianian 12.3k9 gold badges54 silver badges112 bronze badges 3 |3 Answers
Reset to default 11method_name = "delete"
obj.send method_name
Looks like it would be like this in Javascript:
methodName = "delete";
obj[methodName]();
You always need the obj
though, so if in ruby send method_name
is same as self.send method_name
, then you can use this[methodName]()
.
You need to use a bracket notation
b.on("click", window[method_name](event))
If your method was defined in global scope, e.g.
function func_delete(arg1, arg2) {
// ...
}
just use square brackets notation:
var method_name = "func_delete";
window[method_name](arg1, arg2);
Otherwise you may use custom object properties in the same way:
var methods = {
func_delete: function() { ... }
};
methods[method_name]();
obj
. – Esailija Commented Jul 23, 2013 at 8:27