Dynamic load, cache, update and reuse of JS (ii)

Source: Internet
Author: User
Tags end functions log reference

The last one was sent out after a lot of replies, in this first thank you for your enthusiastic supporters! Some recommend the third party framework, such as In.js, Requriejs, Sea.js, lab.js, etc. This broaden the horizon, previously only know Sea.js, save the trouble of their own search. Also used a little time to simply look at it, because each is a big, have their own ideas, if only simple use, then who will find a can, but I am accustomed to the principle to find out. Because I feel that although I do not know the principle can also be used, but know the principle, you can use better .

The main look is sea.js, the current simple understanding is: a loading JS mechanism + Modular Programming (CMD specification) concept. This is the use of Taobao, must be very strong, very strong. So, am I supposed to use it? It depends on what I really want and how much it changes.

So what do I want? The first step just want a can dynamically load JS code, the simpler the better. Why, then? The simpler it is, the less it needs to be modified. Why do you ask for no changes? Because I want to achieve the effect is that each page only need <script type= "Text/javascript" src= "/boot.js" ></script> such a line, you can put all the common JS files are loaded in all , and can be updated automatically.

Do not know if you have found a problem, boot.js can handle other JS file updates, but his own update how to fix it? There are two methods, one is to add a random number as an argument, and the other is to have a lifetime without modification. I do not want to use the former, because every time to go to the server loading, and the original intention does not match. I want to use the latter, of course, I also know, can not be forever unchanged, can only try to delay the modification of time. So you need--simple. The simpler the less you need to modify, the longer you can stay. So my name is called BOOT. is a simple boot (load) function.

  The second step is to start the real management of JS files. This is the time to consider using a third-party framework, of course, you can write yourself. Because I can use boot.js to make sure which files are loaded and to load the latest files. In the second step, you need to determine a solution. My idea is to do a JS file service. This service is implemented by loading JS, updating JS, loading order (dependent), and reusing.

If we want to do five projects, each project is a separate site, then for the common JS file is how to deal with it? 1, each project site to put a copy, citing their own site. 2, do a separate site to store the common JS, and then other projects are unified here to reference. I'm using the second method, what about you?

Suddenly think of a problem, we write JS in the end to achieve what purpose (effect)? Basic functionality (JQuery, my97, editor, etc.), UI (Easyui, etc.), processing the business logic (what to make a point to judge what). There's something else. Each of us write a JS file, we need to consider what files to refer to? At present, I do the project, by JS file services to handle the JS file loading, and then write the code to handle business logic OK.

There are a lot of things to say, just a little messy. Follow up to the specific code, do not know whether you like.

Ps:

Faced with the problem. Referencing https://github.com/seajs/seajs/issues/547

annoying naming conflict

We set out from a simple habit. When I do a project, I often abstract some common, low-level functions, and separate them into functions such as

function each
			(arr)
			{
  //implementation Code
}

function
			log (str)
			{
  //implementation Code
}

And put these functions in a util.js. When you need to use it, you can introduce the file. All of this worked well and my colleagues were thankful that I had provided such a handy kit.

Until the team gets bigger, people start complaining.

Xiao Yang: I want to define a each method to traverse the object, but the util.js of the page header has already defined one, my only call eachobject, good helpless.

Xiao Gao: I have customized a log method, why is the code written by xiaoming a problem? Somebody help me.

complain more and more. After a heated discussion, the team decided to refer to the Java approach and introduce namespaces to solve the problem. So the code in Util.js became

var
			org
			=
			{};
Org. Coolsite
			=
			{};
Org. Coolsite.utils
			=
			{};

Org. CoolSite.Utils.each
			=
			function
			(arr)
			{
  //implementation Code
};

Org. CoolSite.Utils.log
			=
			function
			(str)
			{
  //implementation code
};

End of reference.

Now I'm using the namespaces, of course, in terms of. NET's habits.

Okay, js file, Ai.

Unknowingly actually wrote so much. Using namespaces is really easy to manage. Especially the management source. Also should learn about CMD, AMD are what, write their own code should also be standardized a little



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.