Details about js asynchronous file loaders _ php instances

Source: Internet
Author: User
This article mainly introduces relevant information about the js asynchronous file loader, which has some reference value. interested friends can refer to this scenario we often encounter, some pages depend on third-party plug-ins. these plug-ins are relatively large and are not suitable for packaging to the main js of the page (assuming we use the cmd method, js will be packaged into a file ), at this time, we usually obtain these plug-in files asynchronously and complete the initialization logic after the download is complete.

Taking image upload as an example, we may use the plug-in plupload. js, so we will write it like this:

!window.plupload ?    $.getScript( "/assets/plupload/plupload.full.min.js?1.1.9", function() {      self._initUploader();    }) :    self._initUploader();    

However, our pages are usually composed of multiple independent modules (components). if both modules A and B depend on plupload. js. do you have to write the above code in both places. If you do this, in plupload. before the js file is downloaded, two requests may be initiated. because the js file is downloaded in parallel, the js file may be downloaded repeatedly, instead of the first download and the second cache content.

Is the case where multiple components on the page depend on vue. js (the scenario where jquery and vue are mixed ):

Therefore, locking is required in actual use, that is, when the script is being loaded, the script should not be repeatedly requested. after loading is complete, the subsequent logic should be executed in sequence. a good tool, promise, is available, it is easy to implement.

// Vue loader var promiseStack = []; function loadvue () {var promise = $. deferred (); if (loadvue. lock) {promiseStack. push (promise);} else {loadvue. lock = true; window. vue? Promise. resolve (): // An error is written here, window. when Vue is true, the lock must be set to false. I changed it later $. getScript ("/assets/vue. min. js? 1.1.9 ", function () {loadvue. lock = false; promise. resolve (); promiseStack. forEach (function (prom) {prom. resolve () ;}) ;}return promise ;}window. loadvue = loadvue;

Then in the dependency on vue. js:

loadvue().then(function() { // do something }); 

Let's look at the request:

Well, it seems that the problem has been solved here, but if there are multiple plug-in dependencies on my page, for example, dependency on plupload. js depends on vue again. js, do I want to write the above code again (how do I feel like I have said this )? Isn't it redundant? Therefore, we need an asynchronous loader generator to help us generate multiple asynchronous loaders.

/*** @ Des: js asynchronous loader * @ param {string} name loader name * @ param {string} global variable * @ param {string} url loading address **/var promiseStack = {}; exports. generate = function (name, global, url) {var foo = function () {if (! PromiseStack [name]) {promiseStack [name] = [];} var promise = $. deferred (); if (foo. lock) {promiseStack [name]. push (promise);} else {foo. lock = true; if (window [global]) {foo. lock = false; promise. resolve ();} else {$. getScript (url, function () {foo. lock = false; promise. resolve (); promiseStack [name]. forEach (function (prom) {prom. resolve () ;}) ;}}} return promise ;}; return foo ;};

Then we can generate an asynchronous loader and assign it to window

// Global loader window. loadvue = loader. generate ('Vue ', 'Vue', '/assets/vue/Vue. min. js '); window. loadPlupload = loader. generate ('plupload', 'plupload', '/assets/plupload. full. min. js ');

The problem is basically solved.

The above is a detailed description of the js asynchronous file loader, hoping to help you learn it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.