Javascript front-end UI framework Kit User Guide-kitjs's dialog box component _ javascript skills

Source: Internet
Author: User
This article takes the kitjs dialog box component as an example to introduce in detail the kitjs component directory, default code template, constructor and initial method, and Kitjs inheritance. The explanation is very detailed and helpful for us to master the kitjs components. As a UI library, Kit has no intention of letting everyone learn about its Core and familiarized themselves with my API. This follow-up learning method has no meaning at all. jQuery is hot today, everyone will learn jQ, and SeaJs will be on fire tomorrow, and everyone will stir up SeaJs. So in KitJs, I specially prepared a syntax sugar (Suger) for jQ users. js). It completely simulates jQ APIs. In addition to implementation and interfaces, it is also convenient for you to directly pick up the Kit components. Of course, as a pure technology Fan, an in-depth understanding of how a technology is implemented is far more interesting than what comes with it. Of course, I don't mind if you just copy the component code of the Kit and complete your KPI out of KPI considerations or the project bonus of the boss's boss, as long as you do not forget to dig a slot when drinking water, when blow water with colleagues, you can also promote a KitJs, I am very grateful to you. At the same time, Kit is also a very young library. Due to the constant development, there are some bugs and browser compatibility problems, which are inevitable. I am limited by myself. In this age of front-end battles, more like-minded friends are welcome to make progress together.

At the same time, today released a kitjs dialog box components, demo address for http://xueduany.github.com/KitJs/KitJs/demo/Dialog/demo.html

(1) Kit directory format

Back to the truth, in KitJs, kit. js is the Core file. It contains some of the most commonly used Dom and Object, inherited operations, and a batch of strings are extended according to the functional division under the same level directory. js, math. js and so on are designed to achieve function expansion in specific directions. Each Independent js file contains a Class constructor and an instance of a global object,

Take kit. js as an example, including the $ Kit class and $ Kit class instance $ kit (starting with $ to avoid conflicts with common variables ),

Other types are linked to $ Kit and $ kit instance, such as math. js, including $ Kit. math class and $ kit. math instance to ensure that only $ Kit and $ kit are contaminated globally. At the same time. js, we define a namespace called $ kit. ui, in the physical directory, use kit. the Widget directory at the same level of js. It is a one-character, multiple directories with uppercase letters

All the directories in the widget directory are kitjs component directories. Each Independent js file only contains the class Constructor (not an instance) of an independent component ), at the same time, it can be compatible with the module mode of commonJs (it can comply with the commles/1.1 specification of CommonJs and the AMD mode transformation. The specific transformation method will be mentioned in detail later)

(2) Default Code Template for Kit components. The comments comply with the jsdoc specifications.

Let's take the dialog box component as an example. Each component is similar to the following

The first is the comments of jsdoc. @ class declares what class it is, @ require xxx. js declares which components it depends on

(3) constructor and Initialization Method

Each class is defined as a constructor in the standard function (config) {} mode. Note that the constructor of each kitjs component reserves a config parameter by default, as the input of personalized configuration,

At the same time, there is a static member in the class constructor, The defaultConfig object, which is used to store the default configuration of the kitjs component.

When using the kitjs component, you must first use the new Instance method new $ kit. ui. dialog. yesOrNo: initialize a new instance object. This is only a js Component Object initialized, and there is no HTML. You need to execute the init method, Create HTML, and add it to the doc, add flesh and blood to the soul.

Some people may ask why not put the init method directly in the constructor, but separately?

1. It is because the parent class needs to be instantiated during inheritance. When the Child class inherits from the parent class, the prototype object of the Child class is set as the new Instance object of the new Instance of the parent class, if the init initialization method is put in the constructor, the HTML of the parent class will be executed directly and spam code will be generated,

2. In the case of lazy loading, HTML code must be executed at the appropriate time, rather than immediately executed at the first initialization.

So the default method for using the kitjs component is

After instantiation, run the init method (the init method returns the current component object, with return code 7)

It can be found that all the API methods in dialog are mounted on prototype, inherited by prototype extension and passed to the instance object.

Observe the constructor code of the $ kit. ui. Dialog. YesOrNo component,

(4) Inheritance of KitJs

He used the $ kit. inherit method to declare the inheritance relationship with the $ kit. ui. Dialog object. Here some people will ask, why should we inherit from the constructor instead of writing it out directly?

The reason is:

1. kitjs is based on prototype to maintain the inheritance relationship.

2. To use a kitjs component, you must instantiate the component object. Each component is created through the new Instance method through the constructor.

So I put the execution of the inheritance relationship in the constructor of the Code, so that when a new component is instantiated, it will follow the Inheritance Method of the constructor of the current component, it inherits the members and methods of its parent class step by step.

When the subclass needs to modify the method of the parent class, it only needs to define a method with the same name in the prototype of the subclass to overwrite the Inheritance method of the parent class.

In terms of naming, kitjs follows the principle that the class name of the parent class is used as the Namespace of the subclass and is continuously linked, for example, $ kit. ui. Dialog, $ kit. ui. Dialog. YesOrNo.

The inheritance Implementation of kitjs is also very simple.

Instantiate a parent class object, copy all the members of the parent class instance to the prototype of the subclass, and reset the prototype constructor of the subclass as the subclass constructor, then, the subclass constructor hangs a link pointing to the parent class through $ kit. inherit method, in the subclass $ kit. ui. dialog. during YesOrNo instantiation, You can inherit the parent class $ kit. ui. all the sub-classes of Dialog do not exist, so it can inherit from static languages.

(5) config Parameters, Coupling of HTML and Css?

Kit component constructor is used to pass in a Map-type parameter, which is always a personalized component. When the kit component is initialized, it will automatically overwrite the default defaultConfig with the config parameter submitted by the user and start initialization.

For any component, the changes in the HTML structure and Css style cannot be avoided.

Kit splits this coupling into the config parameter configuration,

The first is to use the HTML template technology. kit advocates the use of the $ kit. newHTML method to directly root the HTML String and generate the html dom to insert the Document Stream,

Therefore, we extract the approximate HTML content of the component and encapsulate it into an HTML String template, which is stored in defaultConfig of the component. If you need to modify the HTML template, you can use the custom config during initialization, override the template field in the default defaconfig config,

In the coupling decomposition of HTML templates and Css, kit uses the className method in js templates to separate them.

Use $ kit. tpl in the init method to replace the html in config with the xxx in config in the way of $ {xxx }.

At the same time, all styles are set in css,

If you need to switch between multiple skins, you can use config to specify the actual className corresponding to $ {cls} during initialization to modify the template's className to achieve skin replacement.

(6) Summary

Basically, through code analysis on the $ kit. ui. Dialog. YesOrNo component, we have a rough understanding of the component implementation structure of kitjs. In fact, it is not difficult to design a page component, but it is difficult to design a page component that can adapt to various requirements. In various situations, it can be quickly deformed and adapt to development. Kit splits HTML templates and Css, customizes config parameters and ultconfig, and subclass obtains attributes and methods of the parent class through inheritance, at the same time, relevant code is reconstructed according to different business needs, which can basically meet the needs of business UI components at various levels and in various environments flexibly.

KitJs includes the basic library and UI library,
Basic Library: selector, dom, animation, dom Event enhancement, hashtree data structure, io, local storage, multithreading, range, etc.
There is also a suger. js that simulates the jquery operation format.
The UI library includes: enhanced form elements, pop-up layers, media players, verification frameworks, waterfall streams, linkages, slides, calendars, Upload components, template engines, etc.

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.