Escolar Documentos
Profissional Documentos
Cultura Documentos
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 aaron = Ext.create('My.sample.Person', 'Aaron'); aaron.eat("Salad"); // alert("Aaron is eating: Salad");
Ext.define Has the class name, members Ext.create Creates an object of the class defined in Ext.define. Using, the object created using Ext.create, the functions and members of the class defined can be accessed.
CONFIGURATION:
Getters and setters for the members of the classes are automatically create. But they can be overridden using apply. Ext.define('My.own.Window', { /** @readonly */ isWindow: true, config: { title: 'Title Here', bottomBar: { 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 must be a valid non-empty string'); } else { return title; } }, applyBottomBar: function(bottomBar) { if (bottomBar && bottomBar.enabled) { if (!this.bottomBar) { return Ext.create('My.own.WindowBottomBar', bottomBar); } else { this.bottomBar.setConfig(bottomBar); } } } });
config has the members that need overridden apply method. constructor has the initconfig() apply<memberName> has the overridden functionality.
STATICS:
Ext.define('Computer', { statics: { instanceCount: 0, factory: function(brand) { // 'this' in static methods refer to the class itself return new this({brand: brand}); } }, config: { brand: null }, constructor: function(config) { this.initConfig(config); // the 'self' property of an instance refers to its class this.self.instanceCount ++; return this; } }); var dellComputer = Computer.factory('Dell'); var appleComputer = Computer.factory('Mac'); alert(appleComputer.getBrand()); // using the auto-generated getter to get the value of a config property. Alerts "Mac" alert(Computer.instanceCount); // Alerts "2"
statics has some members and facory key. factory - ??? config has the value null for brand - ??? constructor has manipulation.
MVC ACHITECTURE:
Application: Has the global settings
Ext.application({ name: 'AM', appFolder: 'app', launch: function() { Ext.create('Ext.container.Viewport', { layout: 'fit', items: [ { xtype: 'panel', title: 'Users', html : 'List of users will go here' } ] }); } });
application has the app name and app folder. launch has a function o Creates a viewport with auto fit layout o Viewport is a container. items title for the panel, html content in the panel
Controller: Listens to events from views Ext.application({ ... controllers: [ 'Users' ], ... });
Controllers are added to the application class as above. Ext.define('AM.controller.Users', { extend: 'Ext.app.Controller', init: function() { console.log('Initialized Users! This happens before the Application launch function is called'); }
});
When the app loads, the Users controller is automatically loaded (because we specified it in the Application definition above). init function is called just before the Application's launch function. init function is mostly used with Controllers control function.
Ext.define('AM.controller.Users', { extend: 'Ext.app.Controller', init: function() { this.control({ 'viewport > panel': { render: this.onPanelRendered } }); }, onPanelRendered: function() { console.log('The panel was rendered'); } });
init function has the control function which component query language. Allows us to pass a CSS-like selector that will find every matching component on the page.
So, whenever any component that matches our selector fires a render event, our onPanelRendered function is called. And the console shows The panel was rendered.
Add view to the global application class. Ext.define('AM.controller.Users', { extend: 'Ext.app.Controller', views: [ 'user.List' ], init: ... onPanelRendered: ... });
Ext.define('AM.view.user.List' ,{ extend: 'Ext.grid.Panel', alias : 'widget.userlist', title : 'All Users', initComponent: function() { this.store = { fields: ['name', 'email'], data : [ {name: 'Ed', email: 'ed@sencha.com'}, {name: 'Tommy', email: 'tommy@sencha.com'} ] }; this.columns = [ {header: 'Name', dataIndex: 'name', flex: 1}, {header: 'Email', dataIndex: 'email', flex: 1} ]; this.callParent(arguments); } });
extend an Ext Panel. alias widget.<something>. Can be used as a xtype. initComponent Has the store and columns values. callParent - ???
Now, the application object is configured to display userlist to the viewport (by giving it in xtype key).
Controlling Grid: Handle double click event to edit the user. Ext.define('AM.controller.Users', { extend: 'Ext.app.Controller', views: [ 'user.List' ], init: function() { this.control({ 'userlist': { itemdblclick: this.editUser } }); }, editUser: function(grid, record) { console.log('Double clicked on ' + record.get('name')); } });
In the controller, the init function, control overridden function, on itemdblclick, this.edituser is called. Component query has also been changed.
Edit:
Ext.define('AM.view.user.Edit', { extend: 'Ext.window.Window', alias : 'widget.useredit', title : 'Edit User', layout: 'fit', autoShow: true, initComponent: function() { this.items = [ { xtype: 'form', items: [ { xtype: 'textfield', name : 'name', fieldLabel: 'Name' }, { xtype: 'textfield', name : 'email', fieldLabel: 'Email' } ] } ]; this.buttons = [ { text: 'Save', action: 'save' }, { text: 'Cancel', scope: this, handler: this.close } ]; this.callParent(arguments); } });
Ext.define('AM.controller.Users', {
extend: 'Ext.app.Controller', views: [ 'user.List', 'user.Edit' ], init: function() { this.control({ 'userlist': { itemdblclick: this.editUser } }); }, editUser: function(grid, record) { var view = Ext.widget('useredit'); view.down('form').loadRecord(record); } });
editUser , we create useredit object. we created the view using the convenient method Ext.widget, which is equivalent toExt.create('widget.useredit'). view.down('form').loadRecord(record); - we used ComponentQuery once more to quickly get a reference to the edit window's form. Every component in Ext JS 4 has a down function, which accepts a ComponentQuery selector to quickly find any child component.
extend Store 9
data has key value pair data Include the Store in the Controller.
Why? Store get automatically loaded onto the page and given a storeId, which makes them really easy to reference in our views.
Creating Model:
Ext.define('AM.model.User', { extend: 'Ext.data.Model', fields: ['name', 'email'] });
//the Users controller will make sure that the User model is included on the page and available to our app Ext.define('AM.controller.Users', { extend: 'Ext.app.Controller', stores: ['Users'], models: ['User'], ... });
Ext.define('AM.controller.Users', { init: function() { this.control({ 'viewport > userlist': { itemdblclick: this.editUser }, 'useredit button[action=save]': { click: this.updateUser } }); }, updateUser: function(button) { console.log('clicked the Save button'); } });
It uses the 'useredit' xtype that we defined above to focus in on our edit user window, and then looks for any buttons with the 'save' action inside that window. When we defined our edit user window we passed {action: 'save'} to the save button. 11
button.up('window') to get a reference to the Edit User window. win.down('form') to get the form.
data has been removed. proxy - Proxies are used by Stores to handle the loading and saving of Model data. url has the JSON object. autoload:true - means the Store will ask its Proxy to load that data immediately.
12
{ success: true, users: [ {id: 1, name: 'Ed', email: 'ed@sencha.com'}, {id: 2, name: 'Tommy', email: 'tommy@sencha.com'} ] }
13