【发布时间】:2014-06-05 09:43:50
【问题描述】:
每次都会显示一个模板,我想做一些事情——例如警报。
以下是这些模板的简约示例:
<script type="text/x-handlebars">
{{outlet}}
</script>
<script type="text/x-handlebars" id="index">
index template. {{#link-to "other"}} go to other Template {{/link-to}}
</script>
<script type="text/x-handlebars" id="other">
other template {{#link-to "index"}} go to index {{/link-to}}
</script>
我尝试了以下方法,但这两个版本都不起作用。
App.Router.map(function () {
this.resource('other');
});
App.IndexController = Ember.Controller.extend({
actions: {
loading: function(){ alert("index called"); }
}
});
App.OtherRoute = Ember.Route.extend({
actions: {
loading: function(){ alert("Other called") }
}
});
那么,当显示模板时,触发操作的正确方法是什么?将操作放在链接到帮助器中是没有选择的,因为如果用户打开“/other”而不单击链接(打开 url ...#/other),也应该触发该操作。
【问题讨论】:
-
检查路线中的加载事件。 ember 指南解释了一个类似的案例emberjs.com/guides/routing/loading-and-error-substates。
-
你想在模板渲染时触发动作,还是在路由改变时触发?也许直接覆盖
Em.Route#setupController?还要在视图中寻找didInserElement! -
路线变化时。 setupController 似乎是正确的方法。谢谢
标签: javascript ember.js