How can I trigger an action, when a template is loaded in ember?

时光怂恿深爱的人放手 提交于 2019-12-25 00:39:22

问题


each time, a template is shown, i'd like to do something -- for example an alert.

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") }
    }
});

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).


回答1:


On the Ember.js site about Routing it states:

You can customize the behavior of a route by creating an Ember.Route subclass.

In your case:

App.OtherRoute = Ember.Route.extend({
   setupController: function(){
       alert("foobar");
   }
});


来源:https://stackoverflow.com/questions/24056818/how-can-i-trigger-an-action-when-a-template-is-loaded-in-ember

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