lukkysam
lukkysam

Reputation: 188

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.

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

Upvotes: 0

Views: 809

Answers (1)

AnimiVulpis
AnimiVulpis

Reputation: 2726

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

Upvotes: 2

Related Questions