admin管理员组文章数量:1406060
I have a template I've deeply integrated with Meteor. It uses a scripts.js file that runs a bunch of mands after $(document).ready and $(window).load.
I put scripts.js inside of client/patibility, and it works only if I do a hard refresh of the template. Otherwise, the template doesn't render and the functions don't execute.
Specifically, this code:
// Append .background-image-holder <img>'s as CSS backgrounds
$('.background-image-holder').each(function() {
var imgSrc = $(this).children('img').attr('src');
$(this).css('background', 'url("' + imgSrc + '")');
$(this).children('img').hide();
$(this).css('background-position', 'initial');
});
Much appreciated if you know how I should take it from here.
I have a template I've deeply integrated with Meteor. It uses a scripts.js file that runs a bunch of mands after $(document).ready and $(window).load.
I put scripts.js inside of client/patibility, and it works only if I do a hard refresh of the template. Otherwise, the template doesn't render and the functions don't execute.
Specifically, this code:
// Append .background-image-holder <img>'s as CSS backgrounds
$('.background-image-holder').each(function() {
var imgSrc = $(this).children('img').attr('src');
$(this).css('background', 'url("' + imgSrc + '")');
$(this).children('img').hide();
$(this).css('background-position', 'initial');
});
Much appreciated if you know how I should take it from here.
Share Improve this question asked Aug 25, 2015 at 18:04 eddiewangeddiewang 4155 silver badges21 bronze badges 6-
Why are you putting this script in
patibility
directory? – Tomasz Lenarcik Commented Aug 25, 2015 at 18:11 - where should I be putting it? – eddiewang Commented Aug 25, 2015 at 18:11
- I was told to put all external js files in the patibility dictionary. – eddiewang Commented Aug 25, 2015 at 18:12
- I think that "external" here means "3rd party" so it only reffers to code you cannot edit easily (e.g. minified libraries). – Tomasz Lenarcik Commented Aug 25, 2015 at 18:13
- what are you remendations to integrating the scripts.js file? – eddiewang Commented Aug 25, 2015 at 18:17
2 Answers
Reset to default 4If you want a function to fire off when the DOM is loaded and all images, use this: (be forewarned, this is in ES6)
let imgCount;
let imgTally = 0;
Template.myTemplate.onRendered(function () {
this.autorun(() => {
if (this.subscriptionsReady()) {
Tracker.afterFlush(() => {
imgCount = this.$('img').length;
});
}
});
});
Template.myTemplate.events({
'load img': function (event, template) {
if (++imgTally >= imgCount) {
(template.view.template.imagesLoaded.bind(template))();
}
}
});
Then you just need to declare imagesLoaded
which will get fired off when all images are done.
Template.myTemplate.imagesLoaded = function () {
// do something
};
Place this stuff in the onRendered function of the template.
Template.<name>.onRendered(function(){
//do your jquery business here!
});
For more info check the docs
本文标签: javascript(document)ready(function() with Meteor JS templatesStack Overflow
版权声明:本文标题:javascript - $(document).ready(function() with Meteor JS templates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744965067a2634895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论