渲染模板时,Ember中的全局钩子 [英] Global hook in Ember when template is rendered

查看:84
本文介绍了渲染模板时,Ember中的全局钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当渲染模板时,是否可以在Ember中捕获全局事件?

Is it possible to trap a global event in Ember when a template is rendered?

所以我可以按照每个模板调用初始化脚本。这不是初始化器的候选者 - 我正在尝试在我的应用程序中大约30%的页面上初始化一些jQuery插件,所以这样做一次,而不是重复自己会很棒。

This is so I can invoke initialisation scripts on a per template basis as described. This isn't a candidate for initializers - I'm trying to initialise some jQuery plugins on approx 30% of pages in my application, so doing this once and not repeating myself would be great.

如果没有全局钩子,挂钩正在呈现的模板最合适的位置是什么?我只使用模板,所以我无法在视图上捕获 didInsertElement

Failing a global hook, what is the most appropriate location to hook a template being rendered? I'm only using templates so I can't trap didInsertElement on a View.

推荐答案

我不知道这是否是正确的方式,但是我尝试了你的问题对我感兴趣。以下内容似乎有效: http://emberjs.jsbin.com/pujiqipafe / 1 / edit?html,js,output

I don't know if it's the proper way but i gave it a try as your question interested me. The follow seems to work : http://emberjs.jsbin.com/pujiqipafe/1/edit?html,js,output

基本上我使用路由钩子 renderTemplate 和添加:

Basically i use the route hook renderTemplateand add :

Ember.run.scheduleOnce('afterRender',myfunction);

调用 this.render()

做一个

App.MyJqueryRoute = Ember.Route.Extend({
  renderTemplate: function(){
    this.render();
    var myjquery = function(){
      //do your jQuery stuff here
    };
    Ember.run.scheduleOnce('afterRender', myjquery);
  }
});

然后简单地扩展 App.MyJqueryRoute 而不是 Ember.Route.Extend 在您需要此功能的路线。

Then simply extend App.MyJqueryRouteinstead of Ember.Route.Extend in the routes you need this functionality.

(这是我有一个提议的想法阅读你的文章,我不是一个专家,所以我很乐意听到任何其他解决方案或改进我的解决方案从社区)

(This is a proposal idea i had while reading your post, i'm not an ember expert so i would be happy to hear any other solutions or improvement of my solutions from the community)

这篇关于渲染模板时,Ember中的全局钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆