3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">// setup an App namespace
9 // This is done to prevent collisions in the global namespace
12 <div id="cfg-Ext.ux.TaskBar.TaskButton-url"></div>/**
14 * @extends Ext.data.Store
15 * @cfg {String} url This will be a url of a location to load the BookStore
16 * This is a specialized Store which maintains books.
17 * It already knows about Amazon's XML definition and will expose the following
24 App.BookStore = function(config) {
25 var config = config || {};
27 reader: new Ext.data.XmlReader({
28 // records will have an "Item" tag
31 totalRecords: '@total'
33 // set up the fields mapping into the xml doc
34 // The first needs mapping, the others are very basic
35 {name: 'Author', mapping: 'ItemAttributes > Author'},
39 // Detail URL is not part of the column model of the grid
43 // call the superclass's constructor
44 App.BookStore.superclass.constructor.call(this, config);
46 Ext.extend(App.BookStore, Ext.data.Store);
50 <div id="prop-Ext.ux.TaskBar.TaskButton-BookGrid"></div>/**
52 * @extends Ext.grid.GridPanel
53 * This is a custom grid which will display book information. It is tied to
54 * a specific record definition by the dataIndex properties.
56 * It follows a very custom pattern used only when extending Ext.Components
57 * in which you can omit the constructor.
59 * It also registers the class with the Component Manager with an xtype of
60 * bookgrid. This allows the application to take care of the lazy-instatiation
61 * facilities provided in Ext's Component Model.
63 App.BookGrid = Ext.extend(Ext.grid.GridPanel, {
65 initComponent : function() {
67 // Pass in a column model definition
68 // Note that the DetailPageURL was defined in the record definition but is not used
69 // here. That is okay.
71 {header: "Author", width: 120, dataIndex: 'Author', sortable: true},
72 {header: "Title", dataIndex: 'Title', sortable: true},
73 {header: "Manufacturer", width: 115, dataIndex: 'Manufacturer', sortable: true},
74 {header: "Product Group", dataIndex: 'ProductGroup', sortable: true}
76 sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
77 // Note the use of a storeId, this will register thisStore
78 // with the StoreMgr and allow us to retrieve it very easily.
79 store: new App.BookStore({
80 storeId: 'gridBookStore',
83 // force the grid to fit the space which is available
88 // finally call the superclasses implementation
89 App.BookGrid.superclass.initComponent.call(this);
92 // This will associate an string representation of a class
93 // (called an xtype) with the Component Manager
94 // It allows you to support lazy instantiation of your components
95 Ext.reg('bookgrid', App.BookGrid);
98 <div id="prop-Ext.ux.TaskBar.TaskButton-BookDetail"></div>/**
101 * This is a specialized Panel which is used to show information about
104 * This demonstrates adding 2 custom properties (tplMarkup and
105 * startingMarkup) to the class. It also overrides the initComponent
106 * method and adds a new method called updateDetail.
108 * The class will be registered with an xtype of 'bookdetail'
110 App.BookDetail = Ext.extend(Ext.Panel, {
111 // add tplMarkup as a new property
113 'Title: <a href="{DetailPageURL}" target="_blank">{Title}</a><br/>',
114 'Author: {Author}<br/>',
115 'Manufacturer: {Manufacturer}<br/>',
116 'Product Group: {ProductGroup}<br/>'
118 // startingMarup as a new property
119 startingMarkup: 'Please select a book to see additional details',
120 // override initComponent to create and compile the template
121 // apply styles to the body of the panel and initialize
122 // html to startingMarkup
123 initComponent: function() {
124 this.tpl = new Ext.Template(this.tplMarkup);
127 background: '#ffffff',
130 html: this.startingMarkup
132 // call the superclass's initComponent implementation
133 App.BookDetail.superclass.initComponent.call(this);
135 // add a method which updates the details
136 updateDetail: function(data) {
137 this.tpl.overwrite(this.body, data);
140 // register the App.BookDetail class with an xtype of bookdetail
141 Ext.reg('bookdetail', App.BookDetail);
144 <div id="prop-Ext.ux.TaskBar.TaskButton-BookMasterDetail"></div>/**
145 * App.BookMasterDetail
148 * This is a specialized panel which is composed of both a bookgrid
149 * and a bookdetail panel. It provides the glue between the two
150 * components to allow them to communicate. You could consider this
151 * the actual application.
154 App.BookMasterDetail = Ext.extend(Ext.Panel, {
155 // override initComponent
156 initComponent: function() {
157 // used applyIf rather than apply so user could
158 // override the defaults
173 itemId: 'detailPanel',
177 // call the superclass's initComponent implementation
178 App.BookMasterDetail.superclass.initComponent.call(this);
180 // override initEvents
181 initEvents: function() {
182 // call the superclass's initEvents implementation
183 App.BookMasterDetail.superclass.initEvents.call(this);
185 // now add application specific events
186 // notice we use the selectionmodel's rowselect event rather
187 // than a click event from the grid to provide key navigation
188 // as well as mouse navigation
189 var bookGridSm = this.getComponent('gridPanel').getSelectionModel();
190 bookGridSm.on('rowselect', this.onRowSelect, this);
192 // add a method called onRowSelect
193 // This matches the method signature as defined by the 'rowselect'
194 // event defined in Ext.grid.RowSelectionModel
195 onRowSelect: function(sm, rowIdx, r) {
196 // getComponent will retrieve itemId's or id's. Note that itemId's
197 // are scoped locally to this instance of a component to avoid
198 // conflicts with the ComponentMgr
199 var detailPanel = this.getComponent('detailPanel');
200 detailPanel.updateDetail(r.data);
203 // register an xtype with this class
204 Ext.reg('bookmasterdetail', App.BookMasterDetail);
207 // Finally now that we've defined all of our classes we can instantiate
208 // an instance of the app and renderTo an existing div called 'binding-example'
209 // Note now that classes have encapsulated this behavior we can easily create
210 // an instance of this app to be used in many different contexts, you could
211 // easily place this application in an Ext.Window for example
212 Ext.onReady(function() {
213 // create an instance of the app
214 var bookApp = new App.BookMasterDetail({
215 renderTo: 'binding-example'
217 // We can retrieve a reference to the data store
218 // via the StoreMgr by its storeId
219 Ext.StoreMgr.get('gridBookStore').load();