Usage of this in sencha touch

Source: Internet
Author: User

Sencha touch is a JS framework, so the use of this in sencha touch is ultimately the use of this in Js.

However, in sencha touch, the scope parameter provided by many methods can specify the scope of the method running,

This is specified in the method body.

In JS, This is not static. It always points to the object that calls this method.

Therefore, by changing this, you can change the scope of the method operation and call functions in a larger scope.

In the following example, a form panel is used to add a listener to each field in the form panel.

In addlisteners, this indicates form panel,

This. getitems (); get each item, each traverses each item, and add

Processing Function updateformrecord, which is in the scope of form panel,

During each execution, this should point to every item, that is, the item does not have the updateformrecord method,

Execution may fail.

The solution is that the second parameter of each allows you to specify the scope to be executed by the function, and pass in this (Form Panel object ),

Therefore, you can call a function other than the function scope.

----------------------------------------------------------------------

Ext. Define ("myformpanel ",{

...

...

Initialize: function (){
This. callparent (arguments );
// Add listeners
This. addlisteners ();

}

Addlisteners: function (){

Fieldsetitems = This. getitems ();
Fieldsetitems. Each (function (itemfield ){
Itemfield. On ('change', this. updateformrecord, this );
}, This );
}

Updateformrecord: function (field, newvalue, oldvalue ){
VaR record = This. getrecord ();
If (Record & (newvalue! = Oldvalue )){
// Set value to record
Record. Set (field. getname (), newvalue );
}

}

})

----------------------------------------------------------------------

 

--- Restore content end ---

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.