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 ---