Extjs self-study tutorial (1): Everything starts from API

Source: Internet
Author: User
Question

This series of articles does not focus on the use of extjs in an all-round way, but focuses on how to solve the extjs problem. The writer does not have to give a long story, but the learner can be self-reliant. L
As long as you have basic knowledge about CSS JavaScript and are not very poor at English (refer to this tutorial to learn more about extjs APIs.

Everything starts from API

Many extjs learners think that extjs works very well when they are new to extjs. After a few lines of code, they can get the result done. But when I use it a lot, the complaint begins: How is extjs layout so troublesome. Where are the parameters? I can't figure out how it works, and I will post a lot of posts on the forum. When I found that there were not many workers, I came to the conclusion that extjs is not easy to use. However, there are a lot of people who can solve the problem smoothly with extjs, so don't complain about sour grapes. If you want to improve it, you have to sit down and read the post with peace of mind :)

Before learning, you must first understand the truth: the so-called development experts, solving problems naturally faster than ordinary people. In addition to experience, one of the most competent aspects is that it is faster to search for information than ordinary people. Like a martial arts secret, it is usually not very useful to ordinary people. You have to be "advanced" and learn the "internal strength" first ". For extjs, the "Martial Arts secret" is the API document of extjs. Therefore, to learn extjs, you must first learn how to look at the API. Therefore, the first step is to learn how to read extjs API documentation.

Download the extjs 2.0.2 (the last free version of extjs) Development Kit. After decompression, you can see the following directories:

Adapter extjs is compatible with other JS Development kits, such as jquery, prototype, and Yui.
Integrated with Adobe AIR
Directory created by build jsbuild
This is the secret of docs martial arts.
Some official extjs examples of examples eye-catching can be used to draw samples based on gourd
Source jsbuild source, which has a JSB project file, you can build your own Ext-all.js
Resources extjs run the required resource files, including icons and CSS.

If the extjs you downloaded has these non-empty directories, congratulations, at least 80% may prove that the extjs you downloaded is not a cottage version or a castrated version. You can learn more about the HTTP environment required for viewing extjs APIs. Here we recommend liteserve. The green software is small and functional. Of course you can also use Apache or IIS, and then point the web root directory to the extjs decompression directory, such as: D:/ext-2.0.2, port 80 to run. Then, input the address http: // localhost/docs/on the server. After loading, you can see the extjs API interface as follows:

After clicking on the tree on the left, the tab on the right can add the label of the class and display the information of the class in the page. Take Panel as an example:

You can see the extjs package, the original definition file (the file corresponding to the SRC build), the related class, the inherited subclass, the parent class, and an intuitive inheritance tree. Then the following area "panel is..." is the description of this class. This area is very important. Generally, this area of the class may provide examples of this class and areas to be aware.

The following describes configuration, attributes, methods, and events. As shown in the figure above, the link to the four parts is provided at the top of the details: "properties" "methods" "events" "Config options ".
Scroll the mouse to view the four parts in sequence:

Config options appears first. These parameters are parameters of the constructor when the class is created. As follows:

You can see that the content is divided into three columns:

The first column is a small arrow. This parameter description can be folded and expanded.

The second column is the parameter description. If there is still content not displayed in the description area, it will be displayed as "..." at the end of the content. For example, in animcollapse, the arrow in the first column can be expanded. The row of the activeitem parameter is the expanded result. The description of this parameter is displayed. Note that many parameters in config options have the default attribute, that is. When new is used, the parameter objects does not pass in this parameter. By default, this parameter uses the default value, which greatly reduces the number of construction parameters used. For example, in a new panel, you can configure only the HTML attributes except the rendered object, for example, new Ext. panel ({renderto: 'paneldiv ', HTML: 'content '});
Note that when you pass in the config object, do not add more or omit commas. The object Definition Format of IE is strict. The last member of an object cannot end with a comma. Otherwise, an error occurs during JS loading. Example: New Ext. panel ({renderto: 'paneldiv ', HTML: 'content',}); it can be passed in firefox3, but in IE6, it will lead to an error that cannot be compiled.

The third column is the definition class. Because Javascript is also the OO syntax, the subclass also inherits the attributes and methods of the parent class. The previous property or method with an up arrow is inherited. Generally, if you look at the special functions of a class (such as the method related to the collapse collapse of the Panel class ), you need to find difined by as the description line of the current class, which is very convenient.

For the use of config options, it is more convenient to specify the xtype of config options. When extjs generates the sub-object of the container, a new object is not needed, you only need to input the configuration object and specify the xtype in the object. Some containers use the default xtype for items. For example, the default xtype of the input configuration object of Ext. Panel is 'panel ', and the default xtype of the input configuration object of buttons of Ext. Panel is button. For example, the following three statements are equivalent:

New Ext. panel ({renderto: 'paneldiv ', items: [{HTML: 'content 1'}, {HTML: 'content 2'}], buttons: [{text: 'test button '}]}); you can also write it as new Ext. panel ({renderto: 'paneldiv ', items: [{xtype: 'panel', HTML: 'content 1'}, {xtype: 'panel ', HTML: 'content 2'}], buttons: [{xtype: 'button ', text: 'test button'}]}); or use the new method new Ext. panel ({renderto: 'paneldiv ', items: [New Ext. panel ({HTML: 'content 1'}), new Ext. panel ({HTML: 'content 2'})], buttons: [New Ext. button ({text: 'test button '})]});

Obviously, the first method is easier than the other two.

Next is public properties, which defines the member variables of the object. It can be accessed directly by the dot. Generally, this part is read-only. As follows:

 

There is nothing to say about this part. Just check the instructions. The only note is the initialconfig attribute. This is the case.

Initialconfig: Object
This component's initial configuration specification. Read-only.

This attribute can directly access your initialization object, which saves some var variables, for example, passing in the config object, such as toolbar. You can get it directly from the initialconfig attribute.

The next step is public methods, which is the public object method. This part defines the accessible methods of objects.

This section also describes the parameters of the method. For JavaScript, it is relatively "random" and does not support overload, making it very easy to make mistakes when writing parameters. Therefore, you need to pay attention to the parameter types. For different parameter types, the parameter types can be separated by a comma. For example, after the add method is expanded, you can see the detailed usage methods of each parameter. For example, the following describes the object parameters:

A component config object may be passed in order to avoid the overhead of constructing a real Component Object
That is to say, you can directly upload a Config object instead of an ext. Component Object. In this way, you can understand that it is also possible to use a method similar to add ({xtype: 'click', text: 'click.

Therefore, the extjs API is the most comprehensive extjs tutorial, which is better than any tutorial or practice. Of course, there is also a deeper learning part, that is, you have to look at the extjs source code, look at the extjs source code can achieve some more powerful functions than the API, for example, put the ext in formlayout. form. radio. This depends on the source code.

The next part is also very important. To increase the extjs object behavior, in addition to the extension object. The most common thing is to add listeners. You will often see some people writing listener on the internet, without knowing how to use those parameters. Here is the most worth learning. That is, the following:

Public Events

For example, the first line is active: (ext. panel P), that is to say, in the listeners of this object, you can listen to the active method. This method has a parameter P, and the input object is Ext. panel. Generally, if the input parameter name is the current class or its parent class, it refers to the object itself. For example, the name of the 2nd events is clear: ext. container this. But you don't need to name the variable this when using it. Why? The javascript keyword is simple.

The next line is the event occurrence condition: fires after the Panel has been exactly ally activated
This indicates that the event is triggered when the object becomes visible and active.

After reading the API documentation, do you know how to use the event? If you do not know, check the Code:

New Ext. panel ({renderto: 'paneldiv ', Title: 'title', items: [{xtype: 'button', text: 'button'}], listeners: {afterlayout: function (panel, layout) {alert (panel. getxtype ());}}});

Because Active is not a directly called event, you can use the afterlayout event as an example. You can try to use panel to access the method of your new object in the method after afterlayout. You will find that the free space provided by extjs is actually very powerful!

In addition to specifying listeners when creating an object, you can use the on method to dynamically add an object after it is created. API:
On (string eventname, function handler, [object scope], [object options])
If you do not want to listen, you can also remove it using UN:
UN (string eventname, function handler, [object scope])

Another point is worth noting. Although the component objects of extjs all contain DOM elements, there are also methods supported by DOM elements, such as click and resize. However, extjs events are extended. That is, some special methods supported by extjs may be added, and the behavior may be different from the direct Dom call, such as the resize method:
Resize: (ext. Component this, number adjwidth, number adjheight, number rawwidth, number rawheight)
The first element is an extjs object, which is significantly different from html dom elements.

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.