简体   繁体   中英

Setting up dependencies with head.js

I just started using head.js for loading JS files asynchronously with dependencies.

Looking at the API documentation and examples, I see that you can apply labels to JS files, then run some conditions when they are loaded, heres an example in their documentation:

// same as above, but pass files in as an Array
head.load([
    { label1: "file1.js" }, 
    { label2: "file2.js" }], 
    function() {
    // do something
});

// Labels are usually used in conjuntion with: head.ready()
head.ready("label1", function() {
    // do something
});

So basically, that will execute anything in the 2nd block of code (inside the closure), when the file1.js is loaded.

What I wanted to do, is load more files (asynchronously) when other files are loaded..

For example..

  1. First, load the jquery file: jquery.min.ks
  2. After thats successfully loaded, asynchronously load DataTables files: [ dataTables.searchHighlight.min.js, dataTables.conditionalPaging.js, jquery.dataTables.yadcf.js ]
  3. After that, load the main application files: [ myapp_core.js, myapp_whatever.js ]

Heres the code I have now, using head.js, It seems to work ok, but it just doesn't seem right (Having to throw head.load() inside other functions)

// Step #1
head.load(
    // First, load the main JS library
    [
        { jquery_core: "/include/plugins/jquery-1.11.3/jquery-1.11.3.min.js" },
        { jquery_ui: "/include/plugins/jquery-ui-1.10.4/ui/minified/jquery-ui.min.js" }
    ],
    function() {
        console.debug('Loaded jQuery library files, loading jQuery plugin files..');
        jquery_plugins();
    }
);


// Step #2
function jquery_plugins() {
    head.load([
            // Load all the jQuery plugin files
            "/include/plugins/bootstrap-3.2.0/js/bootstrap.min.js",

            "/include/plugins/select2/select2.js",
            "/include/plugins/bootstrap3-editable/js/bootstrap-editable.min.js",
            "/include/plugins/bootstrap3-editable/inputs-ext/address/address.js",
            "/include/plugins/bootstrap3-editable/inputs-ext/typeaheadjs/lib/typeahead.js",
            "/include/plugins/bootstrap3-editable/inputs-ext/typeaheadjs/typeaheadjs.js",
            "/include/plugins/bootstrap3-editable/inputs-ext/wysihtml5/wysihtml5.js",

            "/include/plugins/mustache/mustache.js",

            "/include/plugins/slimscroll/jquery.slimscroll.min.js",
            "/include/plugins/jquery-cookie/jquery.cookie.js",
            "/include/plugins/gritter/js/jquery.gritter.js",
            "/include/plugins/jquery-tooltipster-master/js/jquery.tooltipster.js",
            "/include/plugins/bootstrap-wizard/js/bwizard.js",
            "/include/js/apps.js",
            "/include/js/enhanced-select.jquery.min.js",
            "/include/js/jquery.multi-select.js",
            "/include/plugins/intro/intro.js",
            "/include/plugins/switchery/switchery.min.js",
            "/include/js/jquery.multiselect.js",
            "/include/plugins/parsley/src/parsley.js",
            "/include/plugins/parsley/src/extra/validator/APPcustom.js",
            "/include/plugins/parsley/src/extra/plugin/parsley.remote.js",
            "/include/plugins/bootstrap3-timepicker2/js/bootstrap-timepicker.js",
            "/include/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js",
            "/include/plugins/SamWM/numeric/jquery.numeric.min.js",
            "/include/plugins/moment/moment.min.js",
            "/include/plugins/flot/jquery.flot.min.js",
            "/include/plugins/flot/jquery.flot.time.min.js",
            "/include/plugins/flot/jquery.flot.resize.min.js",
            "/include/plugins/flot/jquery.flot.pie.min.js",
            "/include/plugins/flot/jquery.flot.stack.min.js",
            "/include/plugins/flot/jquery.flot.crosshair.min.js",
            "/include/plugins/flot/jquery.flot.categories.js",
            "/include/plugins/sparkline/jquery.sparkline.js",
            "/include/plugins/bootstrap-tagsinput/bootstrap-tagsinput.min.js",
            "/include/plugins/bootstrap-tagsinput/bootstrap-tagsinput-typeahead.js",
            "/include/plugins/jquery-tag-it/js/tag-it.min.js",
            "/include/plugins/bootstrap-select/bootstrap-select.min.js",
            "/include/plugins/contextMenu/src/jquery.ui.position.js",
            "/include/plugins/contextMenu/src/jquery.contextMenu.js",
            "/include/js/jquery.mask.min.js",
            "/include/plugins/jquery-jstree/dist/jstree.min.js",
            "/include/plugins/jquery.expander.js",
            "/include/plugins/jquery-labelauty/source/jquery-labelauty.js",
            "/include/plugins/jquery-file-upload/js/vendor/jquery.ui.widget.js",

            "/include/plugins/jquery-jscroll/jquery.jscroll.js",
            "/include/js/modernizr.js",
            "/include/plugins/jquery-messi/dist/messi.js",

            "/include/plugins/jquery-tmpl/jquery.tmpl.min.js",

            { jquery_highlight: "/include/plugins/jquery-searchhighlight/jquery.highlight.js" },

            { datatables_core: "/include/plugins/DataTables/minify/datatables.min.js" },
            { datatables_searchHighlight: "/include/plugins/DataTables-Plugins/features/searchHighlight/dataTables.searchHighlight.min.js" },
            { datatables_conditionalPaging: "/include/plugins/DataTables-Plugins/features/conditionalPaging/dataTables.conditionalPaging.js" },
            { datatables_ellipsis: "/include/plugins/DataTables-Plugins/dataRender/ellipsis.js" },
            { datatables_yadcf: "/include/plugins/yadcf-0.8.8/jquery.dataTables.yadcf.js" },

            { ckeditor: "/include/plugins/ckeditor/ckeditor.js" }
        ],
        function() {
            console.log('All jQuery plugins loaded, loading APP js files...');
            APP_js_files();
        });
}

// Step #3
function APP_js_files(){
    head.load([
            // Load the application JS files
            { APP_plugins: "/include/js/APP_plugin.js" },
            { APP_intro: "/include/js/APP_intro.js" },
            { APP_app: "/include/js/APP_app.js" },
            { APP_confirmation: "/include/js/APP_confirmation.js" }
        ],
        function() {
            "use strict";
            console.log('Successfully loaded all APP js files!');

            $.ajaxSetup({
                cache:false
            });

            App.init();


            template.init();
            account.init();
            admin.init();
            assets.init();
            forms.init();
        });
}

The above code is inside init.js, which is loaded via..

<script type="text/javascript" src="/include/js/head.min.js" data-headjs-load="/include/js/init.js"></script>

Is this the right way to do it? The documentation on head.js is pretty limited.

Thanks

当前的实现不支持查询或加载顺序,因为我可以在源代码中找到,这就是为什么您的问题解决方案是最好的解决方案之一。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM