Hello guys. I use linkfy package for convert my url strings to links. I have a code like that
Template.mytemplate.rendered= function ()
{
$(’#mycontent’).linkify({
target: “_blank”
});
};
Jquery does work when page loaded first, but when I refresh the same page, it stop working and my new url links turn back to being strings. How I can fix it?
I had the same kind of problems with jQuery-based plugins. I always thought that “onRendered” means that the DOM has already been rendered and it is safe to run code against it. But this is not true. Sometimes, it works, sometimes not.
I’ve tried several approaches - one of them being Tracker.afterFlush() - and got the best results with one of these two (or a combination of both):
Wrap the jQuery plugin call in a Meteor.timeout() call. This gives Blaze the chance to render the page completely before the plugin call is executed. I read somewhere that a timeout of 0 is enough, but I typically use a somewhat longer delay like 200, as you never know…
Wrap the code including the jQuery plugin in a separate template, including its initialization code. I don’t know why, but this simple trick sometimes helped.