JavaScript 異步加載庫:LoadJS

jopen 9年前發布 | 50K 次閱讀 LoadJS JavaScript開發

LoadJS 是一個微型的異步加載庫,支持流行的瀏覽器,體積非常小,只有 590 字節。

LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code in your <html> and then use theloadjsglobal to manage JavaScript dependencies after pageload.

LoadJS is based on the excellent $script library by Dustin Diaz. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 590 bytes (minified + gzipped).


示例代碼:

// define a dependency bundle
loadjs(['foo.js', 'bar.js'], 'foobar');

// execute code elsewhere when the bundle has loaded
loadjs.ready('foobar', function() {
  // foo.js & bar.js loaded
});

支持的瀏覽器:

  • IE9+

  • Opera 12+

  • Safari 5+

  • Chrome

  • Firefox

  • iOS 6+

  • Android 4.4+

// load a single file
loadjs('foo.js', function() {
  // foo.js loaded
});


// load multiple files
loadjs(['foo.js', 'bar.js'], function() {
  // foo.js & bar.js loaded
});


// add a bundle id
loadjs(['foo.js', 'bar.js'], 'foobar', function() {
  // foo.js & bar.js loaded
});


// add a failure callback
loadjs(['foo.js', 'bar.js'],
       'foobar',
       function() { /* foo.js & bar.js loaded */ },
       function(pathsNotFound) { /* at least one path didn't load */ });


// execute a callback after bundle finishes loading
loadjs(['foo.js', 'bar.js'], 'foobar');

loadjs.ready('foobar', function() {
  // foo.js & bar.js loaded
});


// .ready() can be chained together
loadjs('foo.js', 'foo');
loadjs('bar.js', 'bar');

loadjs
  .ready('foo', function() {
    // foo.js loaded
  })
  .ready('bar', function() {
    // bar.js loaded
  });


// compose more complex dependency lists
loadjs('foo.js', 'foo');
loadjs('bar.js', 'bar');
loadjs(['thunkor.js', 'thunky.js'], 'thunk');


// wait for multiple depdendencies
loadjs.ready(['foo', 'bar', 'thunk'],
             function() {
               // foo.js & bar.js & thunkor.js & thunky.js loaded
             },
             function(depsNotFound) {
               if (depsNotFound.indexOf('foo') > -1) {};  // foo failed
               if (depsNotFound.indexOf('bar') > -1) {};  // bar failed
               if (depsNotFound.indexOf('thunk') > -1) {};  // thunk failed
             });


// use .done() for more control
loadjs.ready('my-awesome-plugin', function() {
  myAwesomePlugin();
});

loadjs.ready('jquery', function() {
  // plugin requires jquery
  window.myAwesomePlugin = function() {
    if (!window.jQuery) throw "jQuery is missing!";
  };

  // plugin is done loading
  loadjs.done('my-awesome-plugin');
});

Directory structure

loadjs/
├── dist
|   ├── loadjs.js
|   └── loadjs.min.js
├── examples/
├── gulpfile.js
├── main.js
├── package.json
├── LICENSE.md
├── README.md
├── src
|   └── loadjs.js
├── test/

Development Quickstart

  1. Install dependencies

  2. Clone repository

    $ git clone git@github.com:muicss/loadjs.git
    $ cd loadjs
  3. Install node dependencies using npm

    $ npm install
  4. Build examples

    $ ./node_modules/.bin/gulp build-examples

    To view the examples you can use any static file server. To use thenodejshttp-server module:

    $ npm install http-server
    $ ./node_modules/.bin/http-server -p 3000

    Then visit http://localhost:3000/examples

Run tests

To run the browser tests first build theloadjslibrary:

$ ./node_modules/.bin/gulp build-test

Then visit http://localhost:3000/test

項目主頁:http://www.baiduhome.net/lib/view/home/1437786280334

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!