2

I'm currently running a compiled scripts.js file that activates a bunch of various UI capabilities (masonry, navbar, etc.) using the onRendered method.

Template.mainLayout.onRendered( function() {
    $.getScript('js/scripts.js');
});

My issue is, if I navigate to another page like /team, the script doesn't rerun, meaning in other pages, the masonry does not work anymore. Additionally, when I come back to the home page, the script also breaks, making the nav bar broken.

Please let me know if I'm using this function wrong?

2
  • Are you using the mainLayout in iron router? Commented Aug 15, 2015 at 4:48
  • Nope I am using flow router and blaze layouts. I am now loading the script directly in the scripts.js file with the same function. Commented Aug 15, 2015 at 16:09

2 Answers 2

1

If you’re hosting the scripts yourself, put them in your client/compatibility folder. Meteor will automatically include them in your app.

If you want to load them from CDN, edit your client/index.html folder to add a script tag.

Then in your onRendered callback, put the code that initializes your custom scripts, e.g. this.$('table').datatable() or whatever.

Sign up to request clarification or add additional context in comments.

3 Comments

Getting to a closer solutionn now. Loading them via client/compatibility, and I've now scoped in the the code that's not being reloaded. However onRendered only initializes my scripts, how can I reinitialize that script on every reload (to a new route)?
If onRendered isn’t rerunning, it’s because your template isn’t rerendering—even if it disappears and returns (i.e. you change routes and return). To force it to rerender, it needs to contain a variable that changes; for example, a helper that returns Date.now() or somesuch. And if that feels hacky, that’s because it is—if you think your template needs to rerender when Meteor thinks it doesn’t need to, something larger is probably wrong. I suggest you post your specific circumstances/code (including jQuery plugins or the like that you may be trying to initialize) in a new question.
I tried this: Template.mainLayout.helpers({ reactivity: Date.now() }); This is just for a learning project, so hacky is good. However, it still doesn't seem to force rerender, even if I set a changing variable.
0

Eddie, .getscript() is an async function, so you need to specify a calback. I needed to load in meteor an external JS for a leaflet plugin. Using async, defining callback functions did the hack:

    $.getScript('js/l.control.geosearch.js', function( data, textStatus, jqxhr ) {
        $.getScript('js/l.geosearch.provider.google.js', function( data, textStatus, jqxhr ) {
            new L.Control.GeoSearch({
                provider: new L.GeoSearch.Provider.Google(),
                position: 'topcenter',
                showMarker: false,
                retainZoomLevel: true,
            }).addTo(map);
        })
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.