最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there an equivalent in JavascriptCoffeescriptjQuery to Ruby's send? - Stack Overflow

programmeradmin5浏览0评论

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
  • What are you sending to in the coffeescript code? in the ruby examples you have obj. – Esailija Commented Jul 23, 2013 at 8:27
  • @Esailija Sorry if that wasn't clear, I'm not using an object in the javascript. Ruby automatically uses self i.e. this, so there's no object needed in the example, I just thought it would be clearer. I'll change it, thanks. – ian Commented Jul 23, 2013 at 8:29
  • "Ruby automatically uses self if no receiver is explicitly given" I should have written above. – ian Commented Jul 24, 2013 at 8:02
Add a comment  | 

3 Answers 3

Reset to default 11
method_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]();
发布评论

评论列表(0)

  1. 暂无评论