0

I would like to turn a primitive event (a click) into a semantic event, like "deleteTodo" This is described here, but not how to implement :(
I have the following code:

App.TodoView = Em.View.extend({
    click: function(e) {
        this.trigger("deleteTodo");
    }
});

App.Router.map(function(match) {
    match('/').to('index');
});

App.IndexRoute = Ember.Route.extend({
    deleteTodo: function(e) {
        // this code is never executed :(
    }
}) ;

After I perform the 'click', I see that the TodoView click function is called, but not the deleteTodo function from the IndexRoute. Any suggestions what might go wrong here ?

CHeers

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

1 Answers1

5

You can use this.get("controller").send("deleteTodo"). This will send a message to the controller, if the controller doesn't handle deleteTodo it will bubble to the router and be handled there.

click: function(e) {
    this.get('controller').send("deleteTodo");
}

In your router you will also need to define the event:

events: {
  doStuff: function(e) {
    alert("Do stuff") ;    
  }
}

http://jsfiddle.net/9Xasr/7/

I would typically do record deletion in the controller. Seems like putting that in a router event would not be ideal.

Cory Loken
  • 1,395
  • 8
  • 8
  • I can't get it to work, so I've created a jsfiddle here: http://jsfiddle.net/jeanluca/9Xasr/6/ Any suggestions ? – Jeanluca Scaljeri Feb 01 '13 at 08:21
  • Its not this.get('controller').send('deleteTodo') but to send the event to the router we have to use the target property in the controller. e.g. this.get('controller.target').send('deleteTodo'). I have update the fiddle here: http://jsfiddle.net/9Xasr/9/ – guleria May 02 '13 at 11:53
  • When you send an event to a controller it bubbles to the router. I prefer to send to controller in case it wants to handle the event and if I ignore it then it bubbles to the router. – Cory Loken May 02 '13 at 14:59