Emberjs: Jquery Function not being called after Handlebars Template is loaded

落爺英雄遲暮 提交于 2019-12-25 16:31:42

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/19166641/emberjs-jquery-function-not-being-called-after-handlebars-template-is-loaded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!