0

In jquery when you specify a click handler for a button, you can refer to this to get the button that was clicked. However, when you use {{action blah}} this refers to the controller. Is there any way to get that button without having to query for it? Not that it's hard to do $("#myButton") just curious...

App.MyController = Em.Controller.extend({
    buttonClicked:function() {
        //this.?
    }
})
Ben
  • 16,124
  • 22
  • 77
  • 122
  • Possible duplicate of http://stackoverflow.com/questions/11849983/emberjs-get-the-current-element – billyonecan May 02 '13 at 07:37
  • but then I have to create an Ember.View and override it. my action is in a controller. I have a simple html button, seems like I should be able to get that w/out having to write a View extension for it... no? – Ben May 02 '13 at 07:58

2 Answers2

0

{{action}} takes a parameter, besides the action name. So you could do: {{action "blabla" "blabla"}} and use it in:

blabla: function(param) {
  if (param === 'blabla') {
    // action handler here
  }
}
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
harn145
  • 106
  • 5
  • Hi again ham145 :) just tried that with `{{action login this}}` and param is undefined. Am I missing something? – Ben May 02 '13 at 08:01
  • Hi indeed! I use literals, so: `{{action "login" "manual"}}`, and simply process using `login: function( type )`. Not sure on passing objects. – harn145 May 02 '13 at 08:07
0

I believe this calls for a View which will not only simplify your code but also make it easier to maintain. The controller is meant to manipulate data and respond to events with the data you send it. It shouldn't have to know about the view it is associated to.

So on a view you can do:

App.SpecialButtonView = Ember.View.extend
  click: ->
    #Handle your action here

You can then for example do something like @get("controller").send("doSomething") if you have to make something with data.

Neikos
  • 1,840
  • 1
  • 22
  • 35