问题描述
我正在更新一个使用 ember.js 版本 0.9.x 的个人项目.
I'm updating a personal project where I used the ember.js version 0.9.x.
所以发布了一个新版本,我遇到了与 ember action 相关的问题.
So a new version was released and I have a problem related with ember action.
我有以下 html 代码:
I have the following html code:
<li><a href="#" id="startApp" {{action activateView target="view"}}> Home</a> <span class="divider">|</span></li>
在哪里,当我点击它时调用这个函数 activateView:
where, when I click its call this function activateView:
activateView: function(event, context) {
console.log(event);
}
但事件和上下文未定义.我已经尝试过 this.context 并且它返回 undefined.
but the event and the context are undefined. I've already tried this.context and it returns undefined.
主要思想是在用户点击时获取链接的id.
The main idea its obtain the id of the link when the user click.
我知道路线和车把助手链接,但我真的需要这个 ID 来做其他事情,
I know about routes and the handlebar helper link to, but I really need that id for other things,
推荐答案
未使用操作助手传递事件.如果你真的想要事件对象,你需要定义一个视图并使用click
事件:
The event is not passed using the action helper. If you really want the event object, you need to define a view and use the click
event:
App.MyLink = Em.View.extend({
click: function(e) {
}
});
然后:
<li>{{view App.MyLink}}</li>
但是需要访问 dom 事件的情况很少见,因为您可以将参数传递给 {{action}}
.在你的情况下:
but requiring access to the dom event is a rare case, because you can pass arguments to {{action}}
. In your case:
<li><a href="#" id="startApp" {{action activateView "startApp" target="view"}}> Home</a> <span class="divider">|</span></li>
在事件中:
activateView: function(id) {
console.log(id);
}
这篇关于我们如何在 ember 的 action 中获取原始事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!