【问题标题】:How can I trigger an action, when a template is loaded in ember?当模板在 ember 中加载时,如何触发操作?
【发布时间】:2014-06-05 09:43:50
【问题描述】:

每次都会显示一个模板,我想做一些事情——例如警报。

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

<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 指南解释了一个类似的案例emberjs.com/guides/routing/loading-and-error-substates
  • 你想在模板渲染时触发动作,还是在路由改变时触发?也许直接覆盖Em.Route#setupController?还要在视图中寻找didInserElement
  • 路线变化时。 setupController 似乎是正确的方法。谢谢

标签: javascript ember.js


【解决方案1】:

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

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

在你的情况下:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多