Upgrade to ExtJS 4.0.7 - Released 10/19/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="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../resources/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'>/**
19 </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  */
25 Ext.define('Ext.grid.property.Store', {
26
27     extend: 'Ext.data.Store',
28
29     alternateClassName: 'Ext.grid.PropertyStore',
30
31     uses: ['Ext.data.reader.Reader', 'Ext.data.proxy.Proxy', 'Ext.data.ResultSet', 'Ext.grid.property.Property'],
32
33 <span id='Ext-grid-property-Store-method-constructor'>    /**
34 </span>     * Creates new property store.
35      * @param {Ext.grid.Panel} grid The grid this store will be bound to
36      * @param {Object} source The source data config object
37      */
38     constructor : function(grid, source){
39         var me = this;
40         
41         me.grid = grid;
42         me.source = source;
43         me.callParent([{
44             data: source,
45             model: Ext.grid.property.Property,
46             proxy: me.getProxy()
47         }]);
48     },
49
50     // Return a singleton, customized Proxy object which configures itself with a custom Reader
51     getProxy: function() {
52         if (!this.proxy) {
53             Ext.grid.property.Store.prototype.proxy = Ext.create('Ext.data.proxy.Memory', {
54                 model: Ext.grid.property.Property,
55                 reader: this.getReader()
56             });
57         }
58         return this.proxy;
59     },
60
61     // Return a singleton, customized Reader object which reads Ext.grid.property.Property records from an object.
62     getReader: function() {
63         if (!this.reader) {
64             Ext.grid.property.Store.prototype.reader = Ext.create('Ext.data.reader.Reader', {
65                 model: Ext.grid.property.Property,
66
67                 buildExtractors: Ext.emptyFn,
68
69                 read: function(dataObject) {
70                     return this.readRecords(dataObject);
71                 },
72
73                 readRecords: function(dataObject) {
74                     var val,
75                         propName,
76                         result = {
77                             records: [],
78                             success: true
79                         };
80
81                     for (propName in dataObject) {
82                         if (dataObject.hasOwnProperty(propName)) {
83                             val = dataObject[propName];
84                             if (this.isEditableValue(val)) {
85                                 result.records.push(new Ext.grid.property.Property({
86                                     name: propName,
87                                     value: val
88                                 }, propName));
89                             }
90                         }
91                     }
92                     result.total = result.count = result.records.length;
93                     return Ext.create('Ext.data.ResultSet', result);
94                 },
95
96                 // private
97                 isEditableValue: function(val){
98                     return Ext.isPrimitive(val) || Ext.isDate(val);
99                 }
100             });
101         }
102         return this.reader;
103     },
104
105     // protected - should only be called by the grid.  Use grid.setSource instead.
106     setSource : function(dataObject) {
107         var me = this;
108
109         me.source = dataObject;
110         me.suspendEvents();
111         me.removeAll();
112         me.proxy.data = dataObject;
113         me.load();
114         me.resumeEvents();
115         me.fireEvent('datachanged', me);
116     },
117
118     // private
119     getProperty : function(row) {
120        return Ext.isNumber(row) ? this.getAt(row) : this.getById(row);
121     },
122
123     // private
124     setValue : function(prop, value, create){
125         var me = this,
126             rec = me.getRec(prop);
127             
128         if (rec) {
129             rec.set('value', value);
130             me.source[prop] = value;
131         } else if (create) {
132             // only create if specified.
133             me.source[prop] = value;
134             rec = new Ext.grid.property.Property({name: prop, value: value}, prop);
135             me.add(rec);
136         }
137     },
138
139     // private
140     remove : function(prop) {
141         var rec = this.getRec(prop);
142         if (rec) {
143             this.callParent([rec]);
144             delete this.source[prop];
145         }
146     },
147
148     // private
149     getRec : function(prop) {
150         return this.getById(prop);
151     },
152
153     // protected - should only be called by the grid.  Use grid.getSource instead.
154     getSource : function() {
155         return this.source;
156     }
157 });</pre>
158 </body>
159 </html>