首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用head.js设置依赖项

使用head.js设置依赖项
EN

Stack Overflow用户
提问于 2015-09-28 21:58:30
回答 1查看 580关注 0票数 0

我刚刚开始使用head.js异步加载带有依赖项的JS文件。

查看API文档和示例,我看到您可以将标签应用于JS文件,然后在加载它们时运行一些条件,下面是它们的文档中的一个示例:

代码语言:javascript
复制
// 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
});

因此,基本上,在加载file1.js时,这将在第二个代码块(在闭包内)中执行任何操作。

我想做的是,在加载其他文件时(异步地)加载更多的文件。

例如..。

  1. 首先,加载jquery文件: jquery.min.ks
  2. 成功加载后,异步加载DataTables文件: dataTables.searchHighlight.min.js、dataTables.conditionalPaging.js、jquery.dataTables.yadcf.js
  3. 之后,加载主要应用程序文件: myapp_core.js、myapp_whatever.js

下面是我现在的代码,使用head.js,它似乎工作正常,但它似乎不正确(必须将head.load()抛到其他函数中)。

代码语言:javascript
复制
// 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();
        });
}

上面的代码在init.js中,它是通过..。

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

这样做对吗?关于head.js的文档非常有限。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-01-13 12:11:28

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32832476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档