当模板加载到ember时,如何触发动作? [英] How can I trigger an action, when a template is loaded in ember?
问题描述
每次都显示一个模板,我想做一些事情 - 例如一个警报。
each time, a template is shown, i'd like to do something -- for example an alert.
以下是这些模板的简约示例: / p>
Here is a minimalistic example of these templates:
<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>
我尝试了以下操作,但这两个版本都没有工作。
I tried the following, but none of these 2 versions worked.
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 ...#/其他),则将操作放在link-to helper中是不可选的,因为该操作也应该被触发。
So, what's the right right way to trigger an action, when the template is shown? To put the action in the link-to helper is no option, because the action should also be triggered, if the user opens "/other" without clicking a link (opening the url ...#/other).
推荐答案
在Ember.js网站上有关路由它指出:
On the Ember.js site about Routing it states:
您可以通过创建一个Ember来自定义路由的行为
You can customize the behavior of a route by creating an Ember.Route subclass.
在你的情况下:
App.OtherRoute = Ember.Route.extend({
setupController: function(){
alert("foobar");
}
});
这篇关于当模板加载到ember时,如何触发动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!