How to create a class in extjs4
In extjs4, you only need to use one method to solve these problems: Ext. Define. below is its basic syntax: 1:Ext. Define (classname, members, onclasscreated );
Classname: Class Name
Members: the object literal volume of a class member (key-value pair, JSON)
Onclasscreated: the number of optional callback threads. After all dependencies are loaded, the callback class is created and then published. the new asynchronous feature created by the callback class, which is useful in many cases. these will be further discussed in section 4
For example:
Ext. Define ('My. sample. person' , {Name: 'Unknown' , Constructor: Function (Name ){ If (Name ){ This . Name =Name ;} Return This ;}, Eat: Function (Foodtype) {alert ( This . Name + "is eating" + Foodtype ); Return This ;}}); VaR Li = ext. Create ('My. sample. person', 'lilei' ); Li. Eat ( 'Bread ');
ProgramThe "lilei is eating bread" is displayed in the alert window ".
Note: ext. the create () method creates my. sample. A new instance of the person class. we can also use the new keyword (New my. sample. person () to create. however, it is recommended that Ext. create is a habit of creating class examples, because it allows you to take advantage of dynamic loading. for more information about dynamic loading, see Getting Started: getting started
Bytes -----------------------------------------------------------------------------------------
2. Configuration
In extjs 4, we introduced a special Configuration Attribute to provide preprocessing before class creation. features include:
- Configurations completely encapsulate other class members
- Getter and setter. If the class does not define these methodsThe getter and setter methods of the configured attributes are automatically generated..
- Similarly,Each configured property automatically generates an apply method. The automatically generated setter method internally calls the apply method before setting the value.. If you want to customize your own logic before setting the value, reload the apply method.If apply does not return a value, setter does not set a value.. See the following applytitle example:
Sample Code
Ext. Define ('My. Own. window' , {Iswindow: True , Config: {Title: 'Title here' , Bootombar: {enabled: True , Height: 50 , Resizable: False }}, Constructor: Function (Config ){ This . Initconfig (config ); Return This ;}, Applytitle: Function (Title ){ If (! Ext. isstring (title) | title. Length = 0 ) {Alert ( 'Error: Title muset be a valid non-empty string' );} Else { Return Title ;}}, applybottombar: Function (Bottombar ){ If (Bottombar && Bottombar. enabled ){ If (! This . Bottombar ){ // I don't know where the class came out. I didn't define this class. Return Ext. Create ('My. Own. windowbottombar' , Bottombar );} Else { This . Bottombar. setconfig (bottombar );}}}}); VaR Mywindow = ext. Create ('My. Own. window' , {Title: 'Hello world' , Bottombar: {height: 60 }); Alert (mywindow. gettitle ()); // Hello World Mywindow. settitle ( 'Someting new'); Alert (mywindow. gettitle ()); // Something new Mywindow. settitle ( Null ); // Alter "error: title must be a valid non-empty string" Mywindow. setbootombar ({height: 100 }); // The height is set to 100, but why is the bottom here capitalized?
3. Static
Static members can be defined using statics configuration items.
Ext. Define ('computer', {Statics: {instancecount: 0 , Factory: Function (Brand ){ // Brand, trademark // This references the class itself here Return New This ({Brand: Brand}) ;}}, config: {brand: Null }, Constructor: Function (Config ){ This . Initconfig (config ); // Initialize Configuration // Directly drop the static method of the current class This . Self. instancecount ++ ; Return This ;}}); VaR Dellcomputer = computer. Factory ('Dell' ); VaR Aapplecompute = computer. Factory ('mac' ); // Use the automatically generated getter Configuration Attribute Value. Alert "Mac" Alert (aapplecompute. getbrand (); alert (computer. instancecount ); // Alert "2"
Extjs4 treenode: node. Set ('text', 'newvalue ')