Ext JS 4.x and Sencha Touch 2.0 went through a huge refactoring from the ground up with the new class system. It combines the familiar class-based programming style with the dynamic nature of JavaScript into a robust architecture which stands behind every single class written in the frameworks. This session will give you a complete picture of everything our new foundation has to offer as well as their benefits and best practices.
Jacky Nguyen is one of the core engineers behind Sencha frameworks. With his well-rounded knowledge and experiences on both server-side and client-side development as well as industry best practices, he is responsible for designing and improving Sencha frameworks’ overall architectures. He was the author of MooTouch, which has become a part of Sencha Labs. Jacky is passionate about bringing innovation to software development and developers.
9. Namespacing & Code Organization
1. OrganizationName.group[.subgroup].ClassName
2. One class per file
3. File name matches class name
‣ Ext.chart.Label -> Ext/chart/Label.js
‣ Ext.data.writer.Xml -> Ext/data/writer/Xml.js
‣ MyApp.field.Password -> MyApp/field/Password.js
Wednesday, November 2, 11
11. The Good Old Ext.extend()
Ext.ns('My.sample');
My.sample.Person = Ext.extend(Object, {
constructor: function(name) {
this.name = name;
},
walk: function(steps) {
alert(this.name + ' is walking ' + steps + ' steps');
}
});
Wednesday, November 2, 11
12. Introducing Ext.define()
Ext.define('My.sample.Person', {
constructor: function(name) {
this.name = name;
},
walk: function(steps) {
alert(this.name + ' is walking ' + steps + ' steps');
}
});
var tommy = new My.sample.Developer('Tommy');
tommy.walk(5); // alerts 'Tommy is walking 5 steps'
Wednesday, November 2, 11
13. Ext.define() vs Ext.extend()
Ext.define('My.sample.Person', {
constructor: function(name) {
this.name = name;
},
walk: function(steps) {
alert(this.name + ' is walking ' + steps + ' steps');
}
});
‣Class definition is more than just inheritance
‣Automatic namespace allocation
‣Classes are now aware of their own names
‣Asynchronous vs. synchronous
Wednesday, November 2, 11
32. Config: Setter’s Process
✓Validation
✓Filtering
Before ✓Transformation
✓Actual assignment
Set
✓Notification
✓Updating dependencies
After
Wednesday, November 2, 11
33. Config: Magic Methods
getFoo()
return this.foo
setFoo()
Before processedValue = applyFoo(newValue, oldValue)
Set this.foo = processedValue
After updateFoo(processedValue, oldValue)
Wednesday, November 2, 11
34. Config
Ext.define('My.sample.Person', {
/* ... */
applyAge: function(age) {
if (typeof age != 'number' || age < 0) {
alert("Invalid age, must be a positive number");
return;
}
return age;
},
updateAge: function(newAge, oldAge) {
alert("Age has changed from " + oldAge + " to " + newAge);
}
});
var aaron = new My.sample.Person({
name: 'Aaron',
age: -100
}); // alerts "Invalid age, must be a positive number"
alert(aaron.getAge()); // alerts 0
alert(aaron.setAge(35)); // alerts "Age has changed from 0 to 35"
alert(aaron.getAge()); // alerts 35
Wednesday, November 2, 11