2

Possible Duplicate:
emberjs: how to trigger a custom event in a View

I've created a jsfiddle to demonstrate the issue. WHen you click in the view (on the text) you will see in the console that the view receives the click. But when I create a custom event and trigger it, its not received in the router:

App.IndexView = Em.View.extend({
    click: function(e) {
        console.log("CLICK");
        this.get("controller").send("doStuff");
}
});

App.IndexRoute = Em.Route.extend({
    ....
    doStuff: function(e) {
       alert("Do stuff") ;    
    }
}); 

Here is a complete working example too http://jsfiddle.net/jeanluca/9Xasr/6/

Any suggestions ?

Cheers

Community
  • 1
  • 1
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

1 Answers1

3

You are calling doStuff() of the controller for the App.IndexView, but you define doStuff inside the Route. You should move it into App.IndexController:

App.IndexController = Em.Controller.extend({
    doStuff: function(e) {
       alert("Do stuff") ;    
    }
});

Fiddle: http://jsfiddle.net/8k4PE/

On the other hand, if you want the route to receive the event:

<script type="text/x-handlebars" data-template-name="navigation">
    <span {{action "doStuff}}>Sidebar</span>
</script>

App.IndexRoute = Em.Route.extend({
    //...
    events: {
      doStuff: function(e) {
         alert("Do stuff") ;    
      }
    }
}); 
Panagiotis Panagi
  • 9,927
  • 7
  • 55
  • 103
  • From the documentation: These semantic events are sent to your application's router (See the intro on views), so they should travel to your router, I would say! – Jeanluca Scaljeri Feb 04 '13 at 07:43
  • @JeanlucaScaljeri but `doStuff` is not an event handler function. You just call it from the actual event handler (which by default is the `click` function in the view). If you want define `doStuff` as an event handler use `{{action "doStuff"}}` on the target element. – Panagiotis Panagi Feb 04 '13 at 07:52
  • ok, now I see. Just curious, is it possible to trigger the doStuff event manually ? or should this always be done as you just described! – Jeanluca Scaljeri Feb 04 '13 at 08:06
  • @JeanlucaScaljeri I'm not sure what you mean "manually" – Panagiotis Panagi Feb 04 '13 at 08:38
  • Triggering the event within the view class as described above: this.get("controller").send("doStuff"); or this.trigger("doStuff") ; Or maybe this.triggerAction("doStuff") ? – Jeanluca Scaljeri Feb 04 '13 at 09:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23898/discussion-between-jeanluca-scaljeri-and-panagiotis-panagi) – Jeanluca Scaljeri Feb 04 '13 at 12:13
  • @JeanlucaScaljeri you can definitely do that. Also take a look at http://emberjs.com/api/classes/Ember.TargetActionSupport.html – Panagiotis Panagi Feb 05 '13 at 10:41