返回列表

hui.loadjs() 加载模块

默认分类 2015/12/21 21:06

/* 页面加载三种方式,过程分别如下: 方式一 jQuery模式 页面先引入jQuery, 后面再用$(function(){...}) 方式二 AMD/CMD 页面先引入Loader,后面再define('xxx', [], function(){}) 方式三 异步加载Loader 页面先显示Loading再异步加载Loader,再加载模块 */

方式一:不再赘述

方式三:所有外部js文件都在window.onload之后加载

< script type="text/javascript">
'use strict';
var hui = window.hui = window.hui ? window.hui : {};
hui.loadjs = function (url) {
    // 容错处理
    if (!url) return window.console.log('hui.loadjs(url): url is null');
    // 一次加载多个
    if (Object.prototype.toString.call(url) === '[object Array]') {
        for (var i = 0, len = url.length; i < len; i++) hui.loadjs(url[i]); 
        return;
    }
    // 开始加载
    hui.loadjs.loaded = hui.loadjs.loaded || {};
    if (hui.loadjs.loaded[url]) return;
    hui.loadjs.loaded[url] = true;

    var env = '{{config.env}}';
    var s = document.createElement('script');
    url = window.location.href.indexOf('startDebug=true') !== -1 && url.indexOf('/_sourcemap.') === -1 ?
        url.replace(/\.[a-z0-9]{10}\.js/, '.js') : url;
    s.src = url + ((env === 'dev' && window.location.href.indexOf('stopDev=true') === -1) || window.location.href.indexOf('startDev=true') !== -1 ? '?' + (new Date().getTime()) : '');
    document.documentElement.appendChild(s);
};
// 注:资源映射表,从后往前找,新的优先
hui.sourcemap = [];
hui.sourcemapRoute = function (opt) {
    if (opt) opt.replaceExist === false ? hui.sourcemap.unshift(opt) : hui.sourcemap.push(opt);
};
// 基础模块初始化结束后的回调
hui.loadNextCallback = [];
hui.onDefineReady = function (callback) {
    (hui.loadNextLoaded ? callback() : hui.loadNextCallback.push(callback));
};

// 自动加载依赖模块
hui.define_autoload = true;
hui.loadjs('/build/_sourcemap.0b41fbb9f6.js');
hui.loadjs('/build/ymt_core.69d806bac3.js');

< /script>

方式二:基础依赖库直接加载

< script type="text/javascript" src="http://static.bpm.ymatou.com/build/zepto/1.1.6/zepto.54324210f0.js">< /script>
< script type="text/javascript" src="http://static.bpm.ymatou.com/build/ymt_core/1.0.0/ymt_core.js">< /script>

< script>
hui.define_autoload = true;
hui.sourcemapRoute({
    domain: 'http://static.bpm.ymatou.com/build/',
    modules: {
        "base64@1.0.0": "base64/1.0.0/base64.js",
        "hui_basemodel@1.0.0": "hui_basemodel/1.0.0/hui_basemodel.js",
        "hui_boxpanel@1.0.0": "hui_boxpanel/1.0.0/hui_boxpanel.js",
        "hui_btnaddsub@1.0.0": "hui_btnaddsub/1.0.0/hui_btnaddsub.js",
        "hui_button@1.0.0": "hui_button/1.0.0/hui_button.js",
        "hui_control@1.0.0": "hui_control/1.0.0/hui_control.js",
        "hui_imageque@0.0.9": "hui_imageque/0.0.9/hui_imageque.js",
        "hui_template@1.0.0": "hui_template/1.0.0/hui_template.js",
        "hui_util@1.0.0": "hui_util/1.0.0/hui_util.js",
        "jweixin@1.0.0": "jweixin/1.0.0/jweixin.js",
        "jweixin_share@1.0.0": "jweixin_share/1.0.0/jweixin_share.js",
        "md5@1.0.0": "md5/1.0.0/md5.js",
        "mockjs@0.1.9": "mockjs/0.1.9/mockjs.js",
        "swiper@3.1.0": "swiper/3.1.0/swiper.js",
        "ymtapi@1.0.0": "ymtapi/1.0.0/ymtapi.js",
        "ymtapi_unity@1.0.0": "ymtapi_unity/1.0.0/ymtapi_unity.js",
        "ymt_core@1.0.0": "ymt_core/1.0.0/ymt_core.js",
        "zepto@1.1.6": "zepto/1.1.6/zepto.js",
        "zepto_animate@1.0.0": "zepto_animate/1.0.0/zepto_animate.js",
        "zepto_data@1.0.0": "zepto_data/1.0.0/zepto_data.js",
        "zepto_popup@1.0.0": "zepto_popup/1.0.0/zepto_popup.js"
    }
});
(typeof hui !== "undefined") && hui.define && hui.define.loadLeft && hui.define.loadLeft();
< /script>