


Load js dependent modules according to configuration files_javascript skills
Requirements:
According to the configuration file below
module=[
{'name':'jquery','src':'/js/lib/jquery-1.8.3.js'},
{'name':'swfobject','src':'/js/utils/swfobject.js'},
{'name':'fancybox','src':'/js/jquery/jquery.fancybox.js','require':['jquery']},
{'name':'uploadify','src':'/js/utils/uploadify.js','require':['swfobject']},
{'name':'jqform','src':'/js/jquery/jquery.form.js','require':['jquery']},
{'name':'register','src':'/js/page/reg.js','require':['jqform']},
{'name':'login','src':'/js/page/login.js','require':['fancybox','jqform']},
{'name':'upload','src':'/js/page/upload.js','require':['fancybox','jqform','uploadify']}
]
Write a function
def getfiles(name)
Return to load a page specified by a certain name, a list of js files to be loaded, and those with dependencies must be loaded first
Snack solution
This question looks simple at first glance, but it is not.
The difficulty lies in the loading timing of dependent modules. If there is such a dependency relationship: A-B&C, B-C, module A depends on module B and module C, and module B depends on module C, we cannot let C be loaded twice!
The solution given by Xiaocai is just an idea. There must be a better algorithm than this. Xiaocai thinks it can be solved with an algorithm such as a binary tree, but Xiaocai doesn’t know how~~~
This algorithm does not consider circular dependencies.
The code is as follows:
/**
* Does not consider circular dependencies
* @type {Function}
*/
var loadModule = (function(){
/**
* Business logic encapsulation
* @type {{chainHead: {}, chainCurrent: {}, srcCache: {}, main: main, load: load, findModule: findModule}}
*/
var logics = {
Chainhead: {}, // Links
chainCurrent: {}, //Current node of the linked list
srcCache: {}, //module src cache
/**
* External interface
* @param modules Configuration object
* @param name Module name
* @returns {Array} List of dependent modules, arranged in order of loading
*/
main: function(modules, name){
var nameArray = [], //Module name list
Srcarray = [], // List of relying module
nameStr = "", // Module name string set
repeatRegex = /(^| )([w] ).*2/, //Regular module name deduplication
i = 0;
//Roughly load all dependent modules
This.load(modules, name)
//Construct module name string set
This.chainCurrent = this.chainHead;
while(this.chainCurrent.next){
nameArray.push(this.chainCurrent.name);
This.chainCurrent = this.chainCurrent.next;
}
nameStr = nameArray.join(" ") " "; // Unify the standard, add a space at the end
// Deduplication of dependent modules
while(repeatRegex.exec(nameStr)){
nameStr = nameStr.replace(repeatRegex, function(g0, g1, g2){
return g0.substring(0, (g0.length - g2.length));
});
}
nameStr = nameStr.substring(0, (nameStr.length - 1)); //Remove the supplementary extra spaces
//Convert dependent module name to module path
nameArray = nameStr.split(" ");
for(i = 0; i < nameArray.length; i ){
srcArray.push(this.srcCache[nameArray[i]]);
}
return srcArray;
},
/**
* Recursively load modules
* @param modules Configuration object
* @param name Module name
*/
Load: function(modules, name){
var node = {},
module = this.findModule.call(modules, "name", name),
i = 0;
//Determine whether the module exists
If(!module){
throw Error("Dependent module " name " was not found");
}
//Construct module dependency list
node.name = name;
// node.src = module.src;
This.srcCache[name] = module.src;
Node.next = this.chainHead;
This.chainHead = node;
//Recursive dependency
If(module.require && module.require.length){
for(i = 0;i < module.require.length; i ){
This.load(modules, module.require[i]);
}
}
},
/**
* Search the module based on the specified attribute name and attribute value
* @param name Attribute name
* @param value Attribute value
* @returns {*}
*/
findModule: function(name, value){
var array = this,
Item = {},
i = 0;
//Traverse module
for(i = 0; i < array.length; i ){
Item = array[i];
//Get the specified module
If(item && item[name] === value){
return item;
}
}
//Cannot be found and returns null
return null;
}
};
//Expose external interface
Return function(){
return logics.main.apply(logics, arguments);
};
}());
/**
* Test Usecase
* @type {*[]}
*/
var modules=[
{'name':'jquery','src':'/js/lib/jquery-1.8.3.js'},
{'name':'swfobject','src':'/js/utils/swfobject.js'},
{'name':'fancybox','src':'/js/jquery/jquery.fancybox.js','require':['jquery']},
{'name':'uploadify','src':'/js/utils/uploadify.js','require':['swfobject']},
{'name':'jqform','src':'/js/jquery/jquery.form.js','require':['jquery']},
{'name':'register','src':'/js/page/reg.js','require':['jqform']},
{'name':'login','src':'/js/page/login.js','require':['fancybox','jqform']},
{'name':'upload','src':'/js/page/upload.js','require':['fancybox','jqform','login','uploadify']}
];
console.log(loadModule(modules, "upload"));

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
