在ember中加载模板时,如何触发操作?

时间:2014-06-05 09:43:51

标签: javascript ember.js

每次都显示一个模板,我想做点什么 - 例如警报。

以下是这些模板的简约示例:

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

那么,当模板显示时,触发动作的正确方法是什么?将操作放在link-to helper中是没有选择的,因为如果用户打开&#34; / other&#34;也应该触发操作。没有点击链接(打开网址...#/其他)。

1 个答案:

答案 0 :(得分:2)

在关于Routing的Ember.js网站上,它声明:

  

您可以通过创建Ember.Route子类来自定义路径的行为。

在你的情况下:

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