Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / grid / property / Store.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.grid.property.Store
17  * @extends Ext.data.Store
18  * A custom {@link Ext.data.Store} for the {@link Ext.grid.property.Grid}. This class handles the mapping
19  * between the custom data source objects supported by the grid and the {@link Ext.grid.property.Property} format
20  * used by the {@link Ext.data.Store} base class.
21  */
22 Ext.define('Ext.grid.property.Store', {
23
24     extend: 'Ext.data.Store',
25
26     alternateClassName: 'Ext.grid.PropertyStore',
27
28     uses: ['Ext.data.reader.Reader', 'Ext.data.proxy.Proxy', 'Ext.data.ResultSet', 'Ext.grid.property.Property'],
29
30     /**
31      * Creates new property store.
32      * @param {Ext.grid.Panel} grid The grid this store will be bound to
33      * @param {Object} source The source data config object
34      */
35     constructor : function(grid, source){
36         var me = this;
37         
38         me.grid = grid;
39         me.source = source;
40         me.callParent([{
41             data: source,
42             model: Ext.grid.property.Property,
43             proxy: me.getProxy()
44         }]);
45     },
46
47     // Return a singleton, customized Proxy object which configures itself with a custom Reader
48     getProxy: function() {
49         if (!this.proxy) {
50             Ext.grid.property.Store.prototype.proxy = Ext.create('Ext.data.proxy.Memory', {
51                 model: Ext.grid.property.Property,
52                 reader: this.getReader()
53             });
54         }
55         return this.proxy;
56     },
57
58     // Return a singleton, customized Reader object which reads Ext.grid.property.Property records from an object.
59     getReader: function() {
60         if (!this.reader) {
61             Ext.grid.property.Store.prototype.reader = Ext.create('Ext.data.reader.Reader', {
62                 model: Ext.grid.property.Property,
63
64                 buildExtractors: Ext.emptyFn,
65
66                 read: function(dataObject) {
67                     return this.readRecords(dataObject);
68                 },
69
70                 readRecords: function(dataObject) {
71                     var val,
72                         propName,
73                         result = {
74                             records: [],
75                             success: true
76                         };
77
78                     for (propName in dataObject) {
79                         if (dataObject.hasOwnProperty(propName)) {
80                             val = dataObject[propName];
81                             if (this.isEditableValue(val)) {
82                                 result.records.push(new Ext.grid.property.Property({
83                                     name: propName,
84                                     value: val
85                                 }, propName));
86                             }
87                         }
88                     }
89                     result.total = result.count = result.records.length;
90                     return Ext.create('Ext.data.ResultSet', result);
91                 },
92
93                 // private
94                 isEditableValue: function(val){
95                     return Ext.isPrimitive(val) || Ext.isDate(val);
96                 }
97             });
98         }
99         return this.reader;
100     },
101
102     // protected - should only be called by the grid.  Use grid.setSource instead.
103     setSource : function(dataObject) {
104         var me = this;
105
106         me.source = dataObject;
107         me.suspendEvents();
108         me.removeAll();
109         me.proxy.data = dataObject;
110         me.load();
111         me.resumeEvents();
112         me.fireEvent('datachanged', me);
113     },
114
115     // private
116     getProperty : function(row) {
117        return Ext.isNumber(row) ? this.getAt(row) : this.getById(row);
118     },
119
120     // private
121     setValue : function(prop, value, create){
122         var me = this,
123             rec = me.getRec(prop);
124             
125         if (rec) {
126             rec.set('value', value);
127             me.source[prop] = value;
128         } else if (create) {
129             // only create if specified.
130             me.source[prop] = value;
131             rec = new Ext.grid.property.Property({name: prop, value: value}, prop);
132             me.add(rec);
133         }
134     },
135
136     // private
137     remove : function(prop) {
138         var rec = this.getRec(prop);
139         if (rec) {
140             this.callParent([rec]);
141             delete this.source[prop];
142         }
143     },
144
145     // private
146     getRec : function(prop) {
147         return this.getById(prop);
148     },
149
150     // protected - should only be called by the grid.  Use grid.getSource instead.
151     getSource : function() {
152         return this.source;
153     }
154 });