binding-with-classes.js
8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.panel.*',
'Ext.layout.container.Border'
]);
Ext.Loader.onReady(function() {
Ext.define('Book',{
extend: 'Ext.data.Model',
fields: [
// set up the fields mapping into the xml doc
// The first needs mapping, the others are very basic
{name: 'Author', mapping: 'ItemAttributes > Author'},
'Title',
'Manufacturer',
'ProductGroup',
'DetailPageURL'
]
});
/**
* App.BookStore
* @extends Ext.data.Store
* @cfg {String} url This will be a url of a location to load the BookStore
* This is a specialized Store which maintains books.
* It already knows about Amazon's XML definition and will expose the following
* Record defintion:
* - Author
* - Manufacturer
* - ProductGroup
* - DetailPageURL
*/
Ext.define('App.BookStore', {
extend: 'Ext.data.Store',
constructor: function(config) {
config = config || {};
config.model = 'Book';
config.proxy = {
type: 'ajax',
url: 'sheldon.xml',
reader: Ext.create('Ext.data.reader.Xml', {
// records will have an "Item" tag
record: 'Item',
id: 'ASIN',
totalRecords: '@total'
})
};
// call the superclass's constructor
this.callParent([config]);
}
});
/**
* App.BookGrid
* @extends Ext.grid.Panel
* This is a custom grid which will display book information. It is tied to
* a specific record definition by the dataIndex properties.
*
* It follows a very custom pattern used only when extending Ext.Components
* in which you can omit the constructor.
*
* It also registers the class with the Component Manager with an xtype of
* bookgrid. This allows the application to take care of the lazy-instatiation
* facilities provided in Ext's Component Model.
*/
Ext.define('App.BookGrid', {
extend: 'Ext.grid.Panel',
// This will associate an string representation of a class
// (called an xtype) with the Component Manager
// It allows you to support lazy instantiation of your components
alias: 'widget.bookgrid',
// override
initComponent : function() {
// Pass in a column model definition
// Note that the DetailPageURL was defined in the record definition but is not used
// here. That is okay.
this.columns = [
{text: "Author", width: 120, dataIndex: 'Author', sortable: true},
{text: "Title", flex: 1, dataIndex: 'Title', sortable: true},
{text: "Manufacturer", width: 115, dataIndex: 'Manufacturer', sortable: true},
{text: "Product Group", width: 100, dataIndex: 'ProductGroup', sortable: true}
];
// Note the use of a storeId, this will register thisStore
// with the StoreManager and allow us to retrieve it very easily.
this.store = new App.BookStore({
storeId: 'gridBookStore',
url: 'sheldon.xml'
});
// finally call the superclasses implementation
this.callParent();
}
});
/**
* App.BookDetail
* @extends Ext.Panel
* This is a specialized Panel which is used to show information about
* a book.
*
* This demonstrates adding 2 custom properties (tplMarkup and
* startingMarkup) to the class. It also overrides the initComponent
* method and adds a new method called updateDetail.
*
* The class will be registered with an xtype of 'bookdetail'
*/
Ext.define('App.BookDetail', {
extend: 'Ext.Panel',
// register the App.BookDetail class with an xtype of bookdetail
alias: 'widget.bookdetail',
// add tplMarkup as a new property
tplMarkup: [
'Title: <a href="{DetailPageURL}" target="_blank">{Title}</a><br/>',
'Author: {Author}<br/>',
'Manufacturer: {Manufacturer}<br/>',
'Product Group: {ProductGroup}<br/>'
],
// startingMarup as a new property
startingMarkup: 'Please select a book to see additional details',
bodyPadding: 7,
// override initComponent to create and compile the template
// apply styles to the body of the panel and initialize
// html to startingMarkup
initComponent: function() {
this.tpl = Ext.create('Ext.Template', this.tplMarkup);
this.html = this.startingMarkup;
this.bodyStyle = {
background: '#ffffff'
};
// call the superclass's initComponent implementation
this.callParent();
},
// add a method which updates the details
updateDetail: function(data) {
this.tpl.overwrite(this.body, data);
}
});
/**
* App.BookMasterDetail
* @extends Ext.Panel
*
* This is a specialized panel which is composed of both a bookgrid
* and a bookdetail panel. It provides the glue between the two
* components to allow them to communicate. You could consider this
* the actual application.
*
*/
Ext.define('App.BookMasterDetail', {
extend: 'Ext.Panel',
alias: 'widget.bookmasterdetail',
frame: true,
title: 'Book List',
width: 540,
height: 400,
layout: 'border',
// override initComponent
initComponent: function() {
this.items = [{
xtype: 'bookgrid',
itemId: 'gridPanel',
region: 'north',
height: 210,
split: true
},{
xtype: 'bookdetail',
itemId: 'detailPanel',
region: 'center'
}];
// call the superclass's initComponent implementation
this.callParent();
},
// override initEvents
initEvents: function() {
// call the superclass's initEvents implementation
this.callParent();
// now add application specific events
// notice we use the selectionmodel's rowselect event rather
// than a click event from the grid to provide key navigation
// as well as mouse navigation
var bookGridSm = this.getComponent('gridPanel').getSelectionModel();
('selectionchange', function(sm, rs) {
if (rs.length) {
var detailPanel = Ext.getCmp('detailPanel');
bookTpl.overwrite(detailPanel.body, rs[0].data);
}
})
bookGridSm.on('selectionchange', this.onRowSelect, this);
},
// add a method called onRowSelect
// This matches the method signature as defined by the 'rowselect'
// event defined in Ext.selection.RowModel
onRowSelect: function(sm, rs) {
// getComponent will retrieve itemId's or id's. Note that itemId's
// are scoped locally to this instance of a component to avoid
// conflicts with the ComponentManager
if (rs.length) {
var detailPanel = this.getComponent('detailPanel');
detailPanel.updateDetail(rs[0].data);
}
}
});
// do NOT wait until the DOM is ready to run this
}, false);
// Finally now that we've defined all of our classes we can instantiate
// an instance of the app and renderTo an existing div called 'binding-example'
// Note now that classes have encapsulated this behavior we can easily create
// an instance of this app to be used in many different contexts, you could
// easily place this application in an Ext.Window for example
Ext.onReady(function() {
// create an instance of the app
var bookApp = new App.BookMasterDetail({
renderTo: 'binding-example'
});
// We can retrieve a reference to the data store
// via the StoreManager by its storeId
Ext.data.StoreManager.get('gridBookStore').load();
});