In Meteor JS, how to control Javascript load order in relation to DOM load order? For animations

后端 未结 5 571
孤独总比滥情好
孤独总比滥情好 2021-01-03 07:48

I\'ve got a template that I downloaded:

http://halibegic.com/projects/merlin/

I want to use it in Meteor and I\'m having major issues with

&         


        
相关标签:
5条回答
  • 2021-01-03 08:05

    I've used this pattern

    Template.layout.created = function() {
        var jsLibs = [
            'vendors/Flot/jquery.flot.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.pie.js',
            'vendors/Flot/jquery.flot.time.js',
            'vendors/Flot/jquery.flot.stack.js',
            'vendors/Flot/jquery.flot.resize.js',
            'js/flot/jquery.flot.orderBars.js',
            'js/flot/curvedLines.js',
            'js/flot/date.js',
            'js/flot/jquery.flot.spline.js',
            'js/flot/curvedLines.js',
    
            'vendors/bootstrap/dist/js/bootstrap.min.js',
            'vendors/bootstrap/dist/js/bootstrap.min.js',
            'vendors/fastclick/lib/fastclick.js',
            'vendors/nprogress/nprogress.js',
            'vendors/Chart.js/dist/Chart.min.js',
            'vendors/bernii/gauge.js/dist/gauge.min.js',
            'vendors/bootstrap-progressbar/bootstrap-progressbar.min.js',
            'vendors/iCheck/icheck.min.js',
            'vendors/skycons/skycons.js',
            'js/maps/jquery-jvectormap-2.0.3.min.js',
            'js/moment/moment.min.js',
            'js/datepicker/daterangepicker.js',
            'build/js/custom.min.js',
            'js/maps/jquery-jvectormap-world-mill-en.js',
            'js/maps/jquery-jvectormap-us-aea-en.js',
            'js/maps/gdp-data.js'
        ];
    
        jsLibs.forEach((lib) => {
            $('head').append(`<script type="text/javascript" src="${lib}">`);
        });
    }
    

    Then move all the $(document).ready() calls into Template.layout.onRendered and the click events into Template.layout.events({

    0 讨论(0)
  • 2021-01-03 08:11

    None of the above answers worked for me, so I kept hacking away until finally the following worked:

    Template.layout.rendered = function() {
      // hack: these third party header animation scripts must be inserted at the bottom of body
      $('body').append('<script type="text/javascript" src="assets/js/classie-main.js">');
      $('body').append('<script type="text/javascript" src="assets/js/cbpAnimatedHeader.js">');
    };
    

    I put this in my layout.js file.

    0 讨论(0)
  • 2021-01-03 08:14

    All you need to do is to load the javascript after the template is rendered.

    Template.yourLayout.created = function() {
      $('head').append('<script type="text/javascript" src="assets/js/script.js">');
    }
    

    If you have scripts loaded in $(window).load() or $(document).ready(), remember to get that out as well. You could run them in the promise of $getScript as well. This is your case:

    $.getScript('assets/js/script.js', function() {
          initNavbar();
          initPortfolio();
          initAnimations();
          initTwitterFeed();
          $(".loader .fading-line").fadeOut();
          $(".loader").fadeOut("slow");
    });
    
    0 讨论(0)
  • 2021-01-03 08:14

    Thanks to imslavko for the answer!

    http://docs.meteor.com/#meteor_startup

    On a server, the function will run as soon as the server process is finished starting. On a client, the function will run as soon as the DOM is ready.

    So I put this into my client/views/application/layout.js. It uses the jQuery $.getScript, so you have to make sure that jQuery is loaded before you try this:

    Meteor.startup( function () {
      $.getScript("assets/js/jquery.min.js");
      $.getScript("assets/js/bootstrap.min.js");
      $.getScript("assets/js/isotope.pkgd.min.js");
      $.getScript("assets/js/imagesloaded.min.js");
      $.getScript("assets/js/jquery.scrollTo.min.js");
      $.getScript("assets/js/jquery.nav.min.js");
      $.getScript("assets/js/jquery.appear.min.js");
      $.getScript("assets/js/twitterFetcher.min.js");
      $.getScript("assets/js/script.js");
    });
    

    So all of these files will now load AFTER the DOM loads, and therefore animations work.

    0 讨论(0)
  • 2021-01-03 08:25

    It's clear from here:Meteor Docs

    It is best to write your application in such a way that it is insensitive to the order in which files are loaded, for example by using Meteor.startup, or by moving load order sensitive code into packages, which can explicitly control both the load order of their contents and their load order with respect to other packages. However sometimes load order dependencies in your application are unavoidable.

    When not using special filenames and directories:

    Files in subdirectories are loaded before files in parent directories, so that files in the deepest subdirectory are loaded first, and files in the root directory are loaded last. - Within a directory, files are loaded in alphabetical order by filename. Below is a complete list of special file and directory names that control file load order:

    lib

    After sorting as described above, all files under directories named lib are moved before everything else, preserving their order.

    main.*

    All files that match main.* are moved after everything else, preserving their order.

    0 讨论(0)
提交回复
热议问题