My module Loading System V15

Source: Internet
Author: User
Tags what magic

This improvement follows the previous idea to make the definition module name as short as possible. Then, as long as the request path is correct, the module name will be intelligently corrected internally, automatically add the path. For the names and attributes of these modules, you can view the system attribute @ modules of the framework. Although the improvements made by V14 are very user-friendly, after all, there are still many shortcomings and bugs in the first test. V15 is improved in this respect. The logic of the original Module name correction is to write the define method, and now it is transferred to a method called innerdefine, run the command in the temporarily generated IFRAME sandbox to ensure that no error is corrected. In addition, V15 also renamed many internal functions and private attributes to make them more elegant and easy to understand. The following is the improvement details:

  • The internal array names used to save the list of module names to be processed is renamed to tokens (token set ).
  • Correspondingly, the variables and attribute names originally named name or _ name are also renamed as tokens ).
  • The internal object rets used to collect the return values of each module is renamed as transfer ).
  • The map object used to save the information of each module is renamed mapper.
  • The internal function safeeval used to convert the function generated in the IFRAME execution environment is renamed as assemble ). Assemble has a more single responsibility than the original safeeval. It collects the returned values of the corresponding modules in the dependency list and passes them into the target module for execution.
  • Add the innerdefine method, which is used to process the $. define, which is also equivalent to a generator, but will fix the first parameter (Module name) and add the relative path, in addition, callback is re-parsed as a function instance of the parent window (this is equivalent to the work of the original safeeval), and finally handed over to the define method of the namespace object of the parent window for processing.
  • The internal method loadmodule used to create a sandbox environment to load sub-modules is renamed as load, and the large refactoring is performed in the internal String concatenation part.
  • The internal function _ resolvecallbacks used to determine whether the dependency list of a module is successfully loaded is renamed as _ checkdeps.
  • In addition, the _ checkfail method has been slightly improved to ensure that the "object not found" error is reported after the namespace is renamed.

The idea that V15 uses innerdefine to correct the module name has effectively solved the problem of AMD's anonymous module in the industry. However, as a large-scale development framework, mass framework also needs to consider JS file merging. Therefore, the module name, the first parameter of $. Define, is retained. This is also due to forward compatibility considerations.

The last example shows what magic has been done between V14 and V15. To define a JS module in another folder in Kissy, you must specify the path to prevent duplicate names. For example, the AAA folder has a test module and the BBB folder also has a test module. The definition is as follows:

Kissy. add ('aaa/test', function (){//.... AAA/test omitted}); kissy. add ('bbb/test', function (){//.... BBB/test omitted}); kissy. use ('aaa/test', function (){//.... omitted}); kissy. use ('bbb/test', function (){//.... omitted });

The path can be omitted from the V14 of the mass framework. If you do not consider merging JS files on the server, the module name is nothing, because the path in the request module will be changed to its "correct" module name by innerdefine! This feature is very helpful for restructuring the directory structure in the project.

$. Define ('test', function (){//.... AAA/test omitted}); $. define ('test', function (){//.... BBB/test omitted}); $. require ("Aaa/test", function (){//.... omitted}); $. require ("BBB/test", function (){//.... omitted });

Source Code address

Related links:

My module Loading System V14

My module Loading System v13

My module Loading System V12

My module Loading System V11

My module Loading System V10

My module Loading System V9

My module Loading System V8

My module Loading System V7

My module load system v6

My module Loading System V5

My module Loading System v4

My module Loading System v3

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.