emberjs: how to trigger a custom event in a View

断了今生、忘了曾经 提交于 2019-12-04 06:46:38

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14634622/emberjs-how-to-trigger-a-custom-event-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!