FileLoadTools

Posted 青草圆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FileLoadTools相关的知识,希望对你有一定的参考价值。

/**
 * Created by dev013 on 9/9/14.
 */
var FileLoadTools = (function () {
  var my = {};
  var htmlFile = [];// array for storing name of files loaded
  var jsFile = [];
  var cssFile = [];

  /**
   * @param {Object} file filename
   * @param {Object} fileArray
   * function for judging if the file is loaded or not
   */
  function isLoaded(file, fileArray) {
    for (var i = 0; i < fileArray.length; i++) {
      if (fileArray[i] == file) {
        return true;
      }
    }
    return false;
  }

  function getLastArray(array) {
    return array[array.length - 1];
  }

  /**
   * @param {Object} baseurl
   * @param {Object} names
   * function to load js files by names
   */
  my.loadJsFile = function (names) {
    var name = getLastArray(names);
    if (!isLoaded(name, jsFile)) {
      var url = g_applicationContext + g_jsPath + names.join("/") + ".js";
      $("head").append("<script src=‘" + url + "‘></script>");
      jsFile.push(name);
    }
  };

  /**
   * load js files by jQuery ,support callback after js file loaded.
   * @method loadJsFileByJQ
   * @param {Array} names
   * @param {Function} onload
   */
  my.loadJsFileByJQ = function (names, onload) {
    var name = getLastArray(names);
    if (!isLoaded(name, jsFile)) {
      var url = g_applicationContext + g_jsPath + names.join("/") + ".js";
      jsFile.push(name);
      $.getScript(url, function (data, textStatus, jqxhr) {
        if (typeof onload == ‘function‘) {
          onload.apply();
        }
      });
    }

  };

  my.loadMock = function (name, onload) {
    if (!isLoaded(name, jsFile)) {
      var url = g_applicationContext + g_mockPath + name + ".js";
      jsFile.push(name);
      $("head").append("<script src=‘" + url + "‘></script>");
    }
  }

  my.loadCssFile = function (names) {
    var name = names[0];
    if (!isLoaded(name, cssFile)) {
      var url = g_applicationContext + g_cssPath + name + ".css";
      $("head").append("<link type=‘text/css‘ rel=‘stylesheet‘ href=‘" + url + "‘>");
      cssFile.push(name);
    }
  };

  my.loadRqrFiles = function (names, jqObj) {
    var name = getLastArray(names);
    if (!isLoaded(name, htmlFile)) {
      var url = g_applicationContext + g_htmlPath + names.join("/") + ".html";
      var func = function (data) {
        //after the html file loaded,load the js and css files
        jqObj.append(data);
        htmlFile.push(name);
        my.loadJsFile(names);
        my.loadCssFile(names);
        if (names.length == 1) {
          populateThePageWithLanguageSetting(names.join("-") + "Page");
        } else {
          populateThePageWithLanguageSetting(names.join("-"));
        }
      };

      var errorFunc = function (xhr) {
        alert(xhr.statusText);
      };

      $.ajax({
        async: false,
        url: url,
        type: "GET",
        success: func,
        error: errorFunc
      });
    }
  };
  return my;
}());

/*
 *function for load file needed when menu item clicked
 **/
$.fn.loadPageToContent = function (names) {
  FileLoadTools.loadRqrFiles(names, $(this));
};

  

以上是关于FileLoadTools的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数