簡體   English   中英

將模板加載到余燼中時,如何觸發動作?

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

每次顯示一個模板時,我都想做一些事情-例如警報。

這是這些模板的一個簡單示例:

<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.js網站上有關“ 路由”的信息中指出:

您可以通過創建Ember.Route子類來自定義路由的行為。

在您的情況下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM