Emberjs:加载Handlebars模板后Jquery功能未被调用 [英] Emberjs: Jquery Function not being called after Handlebars Template is loaded

查看:100
本文介绍了Emberjs:加载Handlebars模板后Jquery功能未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EmberJS应用程序,我试图调用一个jquery函数,使用 .css()为一个特定的句柄模板定位一个页面上的一些div元素。我的问题是,当EmberJS在模板中加载到我的应用程序(无论是通过url更改,还是 {{linkTo}} ,jquery函数不会被触发,也就是说,如果我刷新页面,它们就会很好,一切都很好,另外我有一个事件监听器,如果页面改变了宽度,这将导致该函数要重试,这个工作也很好。



我的猜测是,一旦模板初始加载,Ember实际上并没有调用jquery函数,反正要解决这个问题我想知道是否可以尝试使用handlebars直接从我的scripts.js调用函数?谢谢你的帮助!



另外,这是不是没有利用视图?可能只是使用Handlebars和组件?

解决方案

您将需要将代码放在视图的 didInsertElement 方法中。所以例如,如果你的模板是 project / index ,你可以这样做:

  App.ProjectIndexView = Ember.View.extend({
template:'project / index',
didInsertElement:function(){
///你的jQuery代码这里
}
});


I have an EmberJS application where I'm trying to call a jquery function that positions some div elements on a page using .css() for a specific handlebars template.

My problem is that when EmberJS loads in the template to my application (whether by url change, or {{linkTo}}, the jquery functions don't get fired. That said, if I refresh the page, they fire just fine and everything is great. Additionally, I have an event listener for if the page changes width, which will cause the function to refire, and this works fine too.

My guess is that Ember doesn't actually call the jquery function once the template is initially loaded. Anyway to get around this? I was wondering if maybe I should try to use handlebars to directly call a function from my scripts.js? Thanks for your help!

Additionally, Is this possible without leveraging Views? Maybe just with Handlebars and components?

解决方案

You will need to put code like that in the didInsertElement method of your view. So for example if your template is project/index, you would do:

App.ProjectIndexView = Ember.View.extend({
    template: 'project/index',
    didInsertElement: function() {
    /// Your jQuery code here
    }
});

这篇关于Emberjs:加载Handlebars模板后Jquery功能未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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