Meteor loads external scripts conditionally

I am trying to make a multi-page Meteor application (I am using https://github.com/tmeasday/meteor-router ). How can I conditionally load javascripts depending on the content of the page?

This is what I am doing: the main page main.htmlhas {{renderPage}} in the tag <body>used by the Meteor router. Let's say that this page / template is called home, which is one of the pages and has some of its scripts. If not for the Meteor, some of these scripts are in <header>, such as jquery, while others are at the end <body>. I put a partial at the end of the template homecalled homeCustomScriptsand it has the corresponding Template.homeCustomScripts function (so I left a lot of scripts, this is just an example):

Template.homeCustomScripts.created = function() {
  scriptInsert = function () {
    $('head').append('<script type="text/javascript" src="js-plugin/respond/respond.min.js">`
    <script type="text/javascript" src="js-plugin/easing/jquery.easing.1.3.js"></script>`
    <script type="text/javascript" src="js/custom.js"></script>');

  }
}

Inside main.html, I have another partial customScript call, and I have inside Template.customScript.renderedcalls scriptInsert().

, , Chrome - Javascript, , :

enter image description here

, , Meteor JQuery <head>, , Meteor , , , , JQuery- JQuery. .

script Meteor?

0
1

- , ( ) - , , ,


, - , , ( ), javascript 1 javascript - client/compatibility, , . https://github.com/meteor/meteor/blob/master/History.md

, , - , server tests


, ,

Meteor.Router.filters({
  'isCustomScriptLoaded': function(page) {
    if(!Session.equals('isCustomScriptLoaded', true)){
        Session.set('isCustomScriptLoaded', true);
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.async = true;
        script.src = "<EXTERNAL SCRIPT URL>";

        var firstScript = document.getElementsByTagName('script')[0];
        firstScript.parentNode.insertBefore(script, firstScript);
    }
  }
});

Meteor.Router.filter('isCustomScriptLoaded',{only: ['pageThatNeedsCustomScript'] });

script - , ,

- script, - .

+1

Source: https://habr.com/ru/post/1534270/


All Articles