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