Implementation of Qomolangma (II): Implementation of namespaces and alias subsystems

Source: Internet
Author: User

========================================================== ==========================================================
Qomolangma openproject V1.0

Category: rich Web Client
Key words: JS Oop, JS framwork, rich Web Client, Ria, Web component,
Dom, dthml, CSS, JavaScript, JScript

Project Initiation: aimingoo (aim@263.net)
Project Team: aimingoo, Leon (pfzhou@gmail.com)
Contributor: Jingyu (zjy@cnpack.org)
========================================================== ==========================================================

1. namedsystem module Overview
~~~~~~~~~~~~~~~~~~

Namedsystem is an optional loading module of qomo. This module provides three functions:
-Enhancements to $ import () in path Recognition
-Namespace subsystem Loading
-Alias subsystem Loading

// Todo: namedsystem. JS is Firefox compatible.

Ii. Composition and loading of the namedsystem Module
~~~~~~~~~~~~~~~~~~

The naming system is divided into the preceding three parts, but their importance is different.

$ Import () adds a standard JavaScript Object: URL () to enhance path recognition (). Thereafter
An anonymous function is executed to repeat the functions in $ import. The restatement of $ import () and the URL () pair
The implementation of these two functions is required for general systems.

The namedsystem module is loaded into the namespace and alias sub-system modules. However, these two modules are optional.
In the qomo system, namespaces or related functions are not forcibly used. In fact, in most cases,
The qomo namespace system is self-maintained.

Code structure of the namedsystem module:
----------
Url = function (){
// URL object implementation
}();

Void = function (){
// Repeat $ import ()
}();

// Load the namespace and alias sub-systems
$ Import ('namespace. js ');
$ Import ('Alias. js ');

// Namespace and alias Declaration
$ Import ('qomo. SPC ');
$ Import ('qomo. alias ');
// More...
----------

3. url object analysis
~~~~~~~~~~~~~~~~~~

In most people's opinion, parsing a URL is very simple. But if you look at the subkeys of the key in the registry:
[Hkey_classes_root/protocols/handler]
You don't think this is a simple task.

This key describes the address protocols that can be supported in IE. IE extends the URL and uses URI to uniformly describe resource files and local machines.
And the content address on the network. The browser is closely integrated with the resource manager and operating system.

For local files, you can use this address protocol to access it in IE (select "file-> open" in IE"
Menu, select the file and confirm it, you can see it in the IE Address Bar ):
File: // C:/Windows/ntbtlog.txt

You can also right-click a Help file (. CHM) and view an "attribute ".
"Url ":
MK: @ msitstore: C:/Windows/help/ups. chm:/MS-ITS: pwrmn. chm:/pwrmn_ups_overview.htm

This address must also be understood by the qomo address system. Because they can both access in IE or HTML pages, of course
JavaScript and qomo are also allowed.

The URL () object uses a regular expression/^ (/W +) (: // *) ([^/] *)/
This expression can get the Protocol format (type) and host address, then the URL () will analyze the whole URL and return
The instance attributes include:
----------
Parse: function URL. parse (){
[Qomo_core Code]
}
URLs: http://sourceforge.net: 80/project/showfiles. php/list? Group_id = 157100 & type = 1
Type: HTTP
HOST: sourceforge.net
Port: 80
Query:/project/showfiles. php
Path:/List
Param: group_id = 157100 & type = 1
Params: [object]
----------

In the preceding attributes, parse () attributes are displayed strangely. In JavaScript, the built-in functions
When it is displayed as a string, the source code is hidden. For example, execute document. writeln (array ):
----------
Function Array (){
[Native code]
}
----------

Qomo implements this "hidden source code" effect. For example:
----------
URL. parse. tostring = $ qomocorefunction ('url. parse ');
URL. tostring = $ qomocorefunction ('url ');
----------
In this way, they are displayed as similar to the built-in functions of the Javascript system.

In terms of usage, URL () adopts a similar design with the Javascript built-in object Regexp. You can set the URL
Used as a global object instance:
----------
URL. parse ('HTTP: // blog.csdn.net/aimingoo/archive/2006/02/13/597658.aspx ');

For (I in URL)
Document. writeln (I, ':', URL [I], '');
----------

Or use the URL as the object constructor:
----------
Url = new URL ('HTTP: // blog.csdn.net/aimingoo/archive/2006/02/13/597658.aspx ');

For (I in URL)
Document. writeln (I, ':', URL [I], '');

// URL. parse ('HTTP: // sourceforge.net/projects/qomo /');
----------

4. Repeat $ import ()
~~~~~~~~~~~~~~~~~~

In the implementation of system. JS, we will introduce the implementation of $ import. Get/set to keep other subsystems
. The namespace handles this part separately.

1. url Base
~~~~~~
Generally, we use document. URL or window. Location. href to obtain the URL of the current webpage.
Address. However, the problem is that, in this case, there may be parameters, such as. aspx calling the following parameter table. This
This will cause troubles for subsequent analysis. Therefore, in qomo, a technique is used to obtain the actual base URL:
----------
// URL base for current document
VaR base = function (){
VaR El = Document. createelement ('img ');
El. src = '.';
Return El. getattribute ('src', 1 );
}();
----------

Since then, qomo has created a URL () object to analyze the base (PARSE), where the query attribute is
All you need to know: based on the absolute path of the current host (docbase ).

2. Temporary reference)
~~~~~~
$ Import Operation Get/set method, so that external code can use $ import. Get () to get $ import ()
Function reference. Because $ import. onsysinitialized () is called after kernel unit initialization
We can even save a reference to the get/set method:
----------
VaR $ getter = $ import. Get; // reference the temporary get () method
VaR activejs = $ getter ('activejs'); // temporary _ SYS. activejs () Reference
// More...
----------

3. Add the internal attribute of _ sys
~~~~~~
More features are required in the $ import () statement after restatement. These features (preferably) are concentrated on _ sys internal objects
. $ Import. Set () provides the following possibilities:
----------
$ Import. Set ('docbase', docbase );
$ Import. Set ('absbase', absbase );
// More...
----------
In this way, some new attributes are added to the _ sys object. This allows other modules later to perform $ import ()
During restatement, you can access the docbase attribute or the absbase () method.

4. Repeat features
~~~~~~
In the namedsystem module, the transitionurl () method is restated. That is, namedsystem
The TargetUrl parameter in $ import (TargetUrl) is understood again. This completely supports the following features:
-TargetUrl can be a relative path based on system. js (only in the system. js unit)
-TargetUrl can be an absolute path based on the current host. (default behavior)
-TargetUrl can be based on the relative path of the current. js. (Repeat namesystem. JS)
-TargetUrl can be a package under the namespace/alias. (repeat in namespace. JS)

5. namespace Subsystem
~~~~~~~~~~~~~~~~~~

1. What is a namespace?
~~~~~~
At least it looks like a namespace (system) is a great system. Because almost all popular languages
To support it. It seems that if it is not supported, it will not become popular even if it is not popular.

In fact, the namespace is nothing remarkable. If you only want to write a class or a controllable class inheritance
Tree, so you cannot use the namespace. But if you want to integrate several different class libraries or a large number of third-party groups
Package, two classes with the same name may exist in these package. In this case, you need
Put them in different namespaces so that they do not conflict with each other. Therefore, you need to configure the UI. Microsoft. Tree and
A namespace such as UI. Yahoo. Tree exists.

2. namespace in Javascript
~~~~~~
Namespaces in advanced languages support such a feature, for example:
----------
$ Import (ui. Microsoft. Tree );

VaR atree = new tdirectorytree ();
----------
In this case, the system will assume that you are creating a UI. Microsoft. Tree. tdirectorytree tree.

In other words, the advanced language uses the namespace as the "Scope" qualifier. In JavaScript,
The scope is either within the function (or more inner) or outside the function. You cannot specify the name of the scope.
Space. -- In the above example, JavaScript will think that it is creating a tdirectorytree
Tree.

The namespace does not exist in Javascript v1.3. But in Javascript later versions, such as JScript 8 (. NET)
Or in Javascript V2, there is a namespace. -- In fact, the Javascript 2 Specification
The namespace is of type and the first class.

Because the namespace concept does not exist in Javascript v1.x, And the scope limitation is the internal mechanism of the JS interpreter.
So it cannot be changed. Therefore, in Javascript 1.x (implemented by third-party code ),
It is often used only for "extended class inheritance Tree", but not for limited scope.

3. How to Implement namespaces
~~~~~~
It is easy to implement a namespace (with no scope) in JavaScript. Because he actually
Is the full name of a class. In this case, a simple implementation code can be as follows:
----------
// 1. Create a namespace
VaR qomo = {};
Qomo. System = {};
Qomo. system. RTL = {};

// 2. Class, constructor
Function memprof (){
// Object constructor...
}

// 3. namespace class
Qomo. system. RTL. memprof = memprof;

// 4. Use a namespace
Mem = new qomo. system. RTL. memprof ();
----------

It can be seen that (in Javascript) a namespace in the general sense is just a reference of the class constructor.

4. namespace in qomo
~~~~~~
In addition to the above meanings, the namespace in qomo also has another layer of meaning, that is, path identification. For example
If we want to use such code
----------
$ Import ('qomo. system. RTL .*');
// Or
$ Import (qomo. system. RTL );
----------
So our real intention is to load all the files in RTL. That is, the "package loading" function.

Because JavaScript does not have the column (local or remote) Directory capability, a description package is required for "package loading ".
Content files, such as package. xml. Parsing this file and loading it one by one is not complicated, but the problem is
$ Import () in qomo is based on the path system. Therefore, you need to translate qomo. system. RTL into a URL path.
Path.

This kind of work is implemented through registernamespace in the general JavaScript implementation framework.
This registernamespace () can be implemented as a global function or a namespace
Method. Basically, however, a namespace is compared with a URL path.

Qomo also needs to establish such a comparison. However, qomo is not forced to use namespaces, so qomo is not strong.
To use registernamespace. Instead, the "map" system is used.

In the $ map () function, qomo creates a private and unique $ map $ object:
----------
VaR $ map $ = {
// Mapper of all path
// 0.. N: Dynamic Properties with this. insert ()

Signpost: function (p ){...},
Remove: function (p ){...},
Insert: function (p, n ){...}
}
----------

$ Map contains numbers such as 0. N as attributes, and numbers represent the length of the path. For example, "/system/RTL/'Length
It is in the object pointed to by the $ map $ [12] attribute.

Here, JavaScript's automatic type conversion is used. In fact, we are using the $ map $ ['12'] attribute.
This method looks like an array, but $ map $ is "clean" than the array: there are no additional methods or attributes.
In fact, this technique uses path. length as the hash_key to create a hash table.

Next, $ map $ ['12'] stores an object declared in the direct volume method:
----------
SP = {
Paths: New array (),
Names: New array ()
}
----------
First, I do not think that a "what a huge" namespace system will be created in Javascript. Second, I think that
After path. length is used as the hash_key, there will not be many hash collisions. Therefore, in the paths/names
In comparison, I simply used arrays.

5. road signs
~~~~~~
Note that the preceding object uses the variable name Sp (signpost ). I use signpost to describe this map
What does node mean?

In a name <-> path ing system, we can find a phenomenon:
----------
Url1 =/qomo/component/tree/nodetree/
Url2 =/qomo/component/tree/
Url3 =/qomo/component/

$ Map (qomo. component, url3 );
$ Map (qomo. component. Tree, url2 );
$ Map (qomo. component. Tree. nodetree, url1 );
---------
In this comparison, we found that there is no need to store all the comparison tables. We only store qomo. Component and
Url3 comparison, then we can obtain
Path.

This space management method is called signpost )":
---------
$ Map (qomo. component, url3 );
$ MapX ('qomo. component. Tree. nodetree ');
---------

Unlike $ map (), $ mpax () does not require the second path parameter ($ map ). $ MapX () based on the string
Forward lookup. When qomo. component is reached, a valid and existing namespace is found. Then retrieve the ing
URL 3. Next, we can establish a comparison of name-> path:
---------
Qomo. component --> url3
Qomo. component. Tree --> url3 + 'tree /';
Qomo. component. Tree. nodetree --> url3 + 'tree/nodetree /';
---------
We can find that this relationship can be calculated (and therefore do not need to be stored in $ map $ ). The key is that we
You need to find the "road sign": qomo. component.

Therefore, $ map $ does not need to store all the name <--> path mappings. It only needs to store the above
The key "signpost )". Another benefit of doing so is that if we convert the physical
Location transfer, we only need to change its path. The relationship between its sub-space and the relevant path does not need to be changed.
Change.

$ Map $. signpost () method, used to find a recent road sign in $ map $ through path. In fact,
It returns the namespace on the signpost. -- I don't want external code to change the roadmap in $ map $,
To do this, use the $ map $. Remove () and $ map $. insert () methods.

6. Calculation of namespace to path
~~~~~~
Simply put, the signpost system is used for searching paths to namespaces. In turn, such
What should we do?

As mentioned above, a namespace is a direct volume of objects. After we have determined the namespace meaning, we
It should also be known that the namespace is an independent system and has nothing to do with the logic of the program code itself. So, we also
Some native attributes in the namespace "object" have no practical significance. For example, constructor.

The only reason for using the constructor attribute is that it is not in the "for... in" loop.
List. In fact, "Basic Object methods" such as tostring () are not listed. But only
Constructor has no definite meaning in an independent system that does not inherit.

Therefore, in the qomo system, many independent systems will use constructor to store some key attributes.
Only to hide the property name, to avoid name conflicts with other third-party systems on the property name. These"
The independent systems in qomo include namespaces, aliases, multi-cast events, and class methods.

In the namespace, we use namespace_object.constructor to store the actual path to which it points. Also
That is to say, "calculation from namespace to path" is just a simple access to the constructor attribute value.

7. Summary
~~~~~~
In a namespace, you can use $ map () to create a namespace and map it to a URL path. This ing
The link is saved as a roadmap in the internal $ map $ object.

The "Path-> namespace" operation is implemented by $ p2n (). The essence is to find the roadmap in $ map $.

The "namespace-> path" operation is implemented by $ n2p (). In essence, it is used to access the constructor attribute of the namespace object.

The namespace can be virtual. In this case, its constructor points to an empty string.

Namespace can be expanded through $ MapX (). In this case, it does not need to save the path in $ map $.

(Most importantly,) the namespace implemented in Javascript 1.x does not have a scope. It is essentially
The full name, qualifier, and path information ing of the object constructor.

6. Alias (alias) subsystem
~~~~~~~~~~~~~~~~~~
Qomo has an immature alias system. Although it is available, before using it, you should note that
"Creating an alias for a namespace does not affect the sub-namespace ".

In addition to this restriction, this alias system is very convenient. You can take a look at the implementation code of alias. JS: simple
Single and fast. Haha.

An alias is actually a namespace, but its constructor points to another namespace. About this
Structure, which has been processed in the implementation of $ n2p (), so it can coexist well with the original namespace system.

Therefore (as an example), qomo. Alias demonstrates a simple alias declaration.
---------
$ Alias ('qomo. RTL ', qomo. system. RTL );
---------

This will allow the qomo. RTL namespace to be created and can be used as the alias of qomo. system. RTL. But please note that,
This does not mean that qomo. RTL. memprof will also be the alias of qomo. system. RTL. memprof. -- Alias system child
Invalid space.

This greatly reduces the value of the alias system. In fact, the solution to this problem is simple:
---------
Qomo. RTL = qomo. system. RTL;
---------

-- Use the reference method to create an alias system. However, this may be the name-path in the namespace system.
Maintaining the relationship brings more trouble. Therefore, I (temporarily) gave up this technology. But only in the comments of alias. js
.

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.