Skip to content Skip to sidebar Skip to footer

How Can I Load Js Into My Templates With Meteor/handlebars.js?

I'm working on a project with Meteor. I've tried loading javascript files using script tags but of course that won't work. These same files I've tried loading using script tags, I'

Solution 1:

You can place it in the public folder and then whenever you need it, you can load it using jQuery's getScript as in:

jQuery.getScript( /yourscript.js)

EDIT: If you are using Iron-Router and the javascript is not always required to load (or it IS required to load before the template is rendered) I recommend this clever solution by Manuel Schoebel:

http://www.manuel-schoebel.com/blog/use-meteor-iron-router-waiton-to-load-external-javascript


Solution 2:

Firstly, code placed in the lib folder will be loaded first as opposed to last (as per the docs). For internal scripts, bear in mind that each file has its own scope, unless you put it in the client/compatability folder, in which case anything declared with var will be in global scope. To avoid polluting the global namespace, it's often better to put your modules in your own package, although the same thing can essentially be achieved without bothering with the smart package format, you just need to bear in mind the scoping rules (i.e. that anything declared with var will only be visible within that file, anything declared without var will be in global scope).

For loading external javascript, script tags placed in the head should work fine, but if you need to control the loading order then you have to be a bit more careful. You can either inject a script tag into your page using a rendered callback, which is the simplest option, but a nicer solution is to use iron-router and the waitOn, before or after hooks.


Post a Comment for "How Can I Load Js Into My Templates With Meteor/handlebars.js?"