Search code examples
ember.jseventtriggercustom-event

EmberJs: Custom event not received in the Router


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


Solution

  • 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") ;    
          }
        }
    });