emberjs:如何在视图中触发自定义事件 [英] emberjs: how to trigger a custom event in a View
问题描述
我想将一个原始事件(一次点击)转换为语义事件,如deleteTodo
这被描述这里,但不是如何实现:(
我有以下代码:
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 :(
}
}) ;
执行点击后,我看到TodoView点击功能被调用,但不是来自IndexRoute的deleteTodo函数有什么建议可以在这里出错?
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
推荐答案
您可以使用this.get(controller)。send(deleteTodo),这将向控制器发送一条消息,如果控制器不处理deleteTodo,它将向路由器发出泡沫并在那里处理。
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") ;
}
}
我通常会做记录删除在控制器中。似乎把它放在路由器事件中是不理想的。
I would typically do record deletion in the controller. Seems like putting that in a router event would not be ideal.
这篇关于emberjs:如何在视图中触发自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!