QWrap Introduction: thin trunk

Source: Internet
Author: User
Let's talk about it. QWrap.
The root namespace of QWrap is "QW", the namespace in QWrap is a big hump, QW is the two Humr of "QWrap", and it is also the first two characters of QWrap.
Regarding the naming of QWrap, many people have asked "What does QWrap mean ?"
Query and Wrap, Quick Wrap, Cute Wrap? Understand it as you like.
At the beginning of the naming, I also hesitated a lot of names, but I finally used this. It is linked to the idea and characteristics of this database, and is a new word that won't be named again with others, facilitating search. The disadvantage is: as the root namespace, the "QW" character is not easy to input, especially for those who prefer to press the Shift key on the left hand, it is good in practical applications (for example, typical Youa applications), QW does not need to be input frequently, just like jquery users, and almost does not need to enter the word "jquery.
The root namespace of QWrap is in js/core/core_base.js. This is the first js of qwrap. The content is as follows:

Let's take a look at the content:

View Code

/**
* @ Singleton
* @ Class QW is the default field of QWrap. All core classes should be defined under the QW field.
*/
(Function (){
Var _ previusqw = window. QW;

Var QW = {
/**
* @ Property {string} VERSION number of the script Library
* @ Default $ version $
*/
VERSION: "$ version $ ",
/**
* @ Property {string} RELEASE Number of the RELEASE script Library (minor version)
* @ Default $ release $
*/
RELEASE: "$ release $ ",
/**
* @ Property {string} PATH: PATH of the script Library
* @ Type string
*/
PATH: (function (){
Var sTags = document. getElementsByTagName ("script ");
Return sTags [sTags. length-1]. src. replace (// [^ \/] + \/[^ \/] + $ /,"/");
}()),

/**
* Get a namespace
* @ Method namespace
* @ Static
* @ Param {String} sSpace namespace character String. If the namespace does not exist, it is automatically created.
* @ Param {Object} root (Optional) namespace start point. If "." is entered in sSpace, the default value is QW. Otherwise, the default value is window.
* @ Return {any} returns the object corresponding to the namespace
*/
Namespace: function (sSpace, root ){
Var arr = sSpace. split ('.'),
I = 0,
NameI;
If (sSpace. indexOf ('.') = 0 ){
I = 1;
Root = root | QW;
}
Root = root | window;
For (; nameI = arr [I ++];) {
If (! Root [nameI]) {
Root [nameI] = {};
}
Root = root [nameI];
}
Return root;
},

/**
* QW is non-conflicting, and the window. QW variable that may be stolen is restored.
* @ Method noConflict
* @ Static
* @ Return {json} returns the QW namespace
*/
NoConflict: function (){
Window. QW = _ previusqw;
Return QW;
},

/**
* Asynchronously load the script
* @ Method loadJs
* @ Static
* @ Param {String} url Javascript file path
* @ Param {Function} onsuccess (Optional) callback Function after loading Javascript
* @ Param {Option} options (Optional) configuration options, such as charset
*/
LoadJs: function (url, onsuccess, options ){
Options = options | {};
Var head = document. getElementsByTagName ('head') [0] | document.doc umentElement,
Script = document. createElement ('script '),
Done = false;
Script. src = url;
If (options. charset ){
Script. charset = options. charset;
}
Script. onerror = script. onload = script. onreadystatechange = function (){
If (! Done &&(! This. readyState | this. readyState = "loaded" | this. readyState = "complete ")){
Done = true;
If (onsuccess ){
Onsuccess ();
}
Script. onerror = script. onload = script. onreadystatechange = null;
Head. removeChild (script );
}
};
Head. insertBefore (script, head. firstChild );
},
/**
* Load the css style sheet
* @ Method loadCss
* @ Static
* @ Param {String} url Css file path
*/
LoadCss: function (url ){
Var head = document. getElementsByTagName ('head') [0] | document.doc umentElement,
Css = document. createElement ('link ');
Css. rel = 'stylesheet ';
Css. type = 'text/css ';
Css. href = url;
Head. insertBefore (css, head. firstChild );
},


/**
* Throw an exception
* @ Method error
* @ Static
* @ Param {obj} exception object
* @ Param {type} Error (Optional) Error type. The default value is Error.
*/
Error: function (obj, type ){
Type = type | Error;
Throw new type (obj );
}
};

/*
* @ Class Wrap package. Add an external skin outside the object
* @ Namespace QW
* @ Param {any} core wrapped object
* @ Return {Wrap}
*/
/*
QW. Wrap = function (core ){
This. core = core;
};
*/

Window. QW = QW;
}());

The main function of this file is to generate the QWrap namespace and define several basic attributes and methods:
Its code is simplified as follows:
(Function (){
Var _ previusqw = window. QW; // record the old QW variable

Var QW = {
VERSION: "$ version $", // string script library VERSION
RELEASE: "$ release $", // RELEASE Number of the string script Library (minor version)
PATH: (function () {}(), // PATH of the string script Library
Namespace: function (sSpace, root) {}, // obtain a namespace
NoConflict: function () {}, // QW does not conflict with each other, restoring the window. QW variable that may be used
LoadJs: function (url, onsuccess, options) {}, // asynchronously load the script
LoadCss: function (url) {}, // load the css style sheet
Error: function (obj, type) {}// throw an exception
};

/*
QW. Wrap = function (core) {this. core = core ;}; // this is commented out, which is equivalent to the recommended method of Wrap.
*/

Window. QW = QW; // output root namespace
}());
Some students may question "the title of the landlord is 'slim trunk ', but this trunk is a little fat !"
Yes. For the root namespace, the lean version should be "var QW = {};".
However, QWrap does not pursue "one-step thin", which is too formal. But the pursuit of "slim ".
For example, core_base.js is in the "slim state ".
"Slim" is an important style of QWrap, which means that QWrap can become thin as needed. That is, in theory, you can use tools to cut unused features of QWrap into excess meat.
A typical application is: no dependency on components.

The core can be split, can be grouped, can be thin, can have no dependencies/can transform rendering, all the "can", can be achieved at a low cost, rather than a great cost of "can ".
This elegant flexibility has always been the main style of QWrap.
What makes QWrap so elegant? First, we owe it to the Helper specification. The next article will introduce it in detail.

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.