binding-with-classes.js 8.2 KB

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