Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Store.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-grid-property-Store-method-constructor'><span id='Ext-grid-property-Store'>/**
19 </span></span> * @class Ext.grid.property.Store
20  * @extends Ext.data.Store
21  * A custom {@link Ext.data.Store} for the {@link Ext.grid.property.Grid}. This class handles the mapping
22  * between the custom data source objects supported by the grid and the {@link Ext.grid.property.Property} format
23  * used by the {@link Ext.data.Store} base class.
24  * @constructor
25  * @param {Ext.grid.Grid} grid The grid this store will be bound to
26  * @param {Object} source The source data config object
27  */
28 Ext.define('Ext.grid.property.Store', {
29
30     extend: 'Ext.data.Store',
31
32     alternateClassName: 'Ext.grid.PropertyStore',
33
34     uses: ['Ext.data.reader.Reader', 'Ext.data.proxy.Proxy', 'Ext.data.ResultSet', 'Ext.grid.property.Property'],
35
36     constructor : function(grid, source){
37         var me = this;
38         
39         me.grid = grid;
40         me.source = source;
41         me.callParent([{
42             data: source,
43             model: Ext.grid.property.Property,
44             proxy: me.getProxy()
45         }]);
46     },
47
48     // Return a singleton, customized Proxy object which configures itself with a custom Reader
49     getProxy: function() {
50         if (!this.proxy) {
51             Ext.grid.property.Store.prototype.proxy = Ext.create('Ext.data.proxy.Memory', {
52                 model: Ext.grid.property.Property,
53                 reader: this.getReader()
54             });
55         }
56         return this.proxy;
57     },
58
59     // Return a singleton, customized Reader object which reads Ext.grid.property.Property records from an object.
60     getReader: function() {
61         if (!this.reader) {
62             Ext.grid.property.Store.prototype.reader = Ext.create('Ext.data.reader.Reader', {
63                 model: Ext.grid.property.Property,
64
65                 buildExtractors: Ext.emptyFn,
66
67                 read: function(dataObject) {
68                     return this.readRecords(dataObject);
69                 },
70
71                 readRecords: function(dataObject) {
72                     var val,
73                         propName,
74                         result = {
75                             records: [],
76                             success: true
77                         };
78
79                     for (propName in dataObject) {
80                         if (dataObject.hasOwnProperty(propName)) {
81                             val = dataObject[propName];
82                             if (this.isEditableValue(val)) {
83                                 result.records.push(new Ext.grid.property.Property({
84                                     name: propName,
85                                     value: val
86                                 }, propName));
87                             }
88                         }
89                     }
90                     result.total = result.count = result.records.length;
91                     return Ext.create('Ext.data.ResultSet', result);
92                 },
93
94                 // private
95                 isEditableValue: function(val){
96                     return Ext.isPrimitive(val) || Ext.isDate(val);
97                 }
98             });
99         }
100         return this.reader;
101     },
102
103     // protected - should only be called by the grid.  Use grid.setSource instead.
104     setSource : function(dataObject) {
105         var me = this;
106
107         me.source = dataObject;
108         me.suspendEvents();
109         me.removeAll();
110         me.proxy.data = dataObject;
111         me.load();
112         me.resumeEvents();
113         me.fireEvent('datachanged', me);
114     },
115
116     // private
117     getProperty : function(row) {
118        return Ext.isNumber(row) ? this.getAt(row) : this.getById(row);
119     },
120
121     // private
122     setValue : function(prop, value, create){
123         var me = this,
124             rec = me.getRec(prop);
125             
126         if (rec) {
127             rec.set('value', value);
128             me.source[prop] = value;
129         } else if (create) {
130             // only create if specified.
131             me.source[prop] = value;
132             rec = new Ext.grid.property.Property({name: prop, value: value}, prop);
133             me.store.add(rec);
134         }
135     },
136
137     // private
138     remove : function(prop) {
139         var rec = this.getRec(prop);
140         if (rec) {
141             store.remove(rec);
142             delete this.source[prop];
143         }
144     },
145
146     // private
147     getRec : function(prop) {
148         return this.getById(prop);
149     },
150
151     // protected - should only be called by the grid.  Use grid.getSource instead.
152     getSource : function() {
153         return this.source;
154     }
155 });</pre>
156 </body>
157 </html>