Upgrade to ExtJS 4.0.0 - Released 04/26/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         this.grid = grid;
21         this.source = source;
22         this.callParent([{
23             data: source,
24             model: Ext.grid.property.Property,
25             proxy: this.getProxy()
26         }]);
27     },
28
29     // Return a singleton, customized Proxy object which configures itself with a custom Reader
30     getProxy: function() {
31         if (!this.proxy) {
32             Ext.grid.property.Store.prototype.proxy = Ext.create('Ext.data.proxy.Memory', {
33                 model: Ext.grid.property.Property,
34                 reader: this.getReader()
35             });
36         }
37         return this.proxy;
38     },
39
40     // Return a singleton, customized Reader object which reads Ext.grid.property.Property records from an object.
41     getReader: function() {
42         if (!this.reader) {
43             Ext.grid.property.Store.prototype.reader = Ext.create('Ext.data.reader.Reader', {
44                 model: Ext.grid.property.Property,
45
46                 buildExtractors: Ext.emptyFn,
47
48                 read: function(dataObject) {
49                     return this.readRecords(dataObject);
50                 },
51
52                 readRecords: function(dataObject) {
53                     var val,
54                         result = {
55                             records: [],
56                             success: true
57                         };
58
59                     for (var propName in dataObject) {
60                         val = dataObject[propName];
61                         if (dataObject.hasOwnProperty(propName) && this.isEditableValue(val)) {
62                             result.records.push(new Ext.grid.property.Property({
63                                 name: propName,
64                                 value: val
65                             }, propName));
66                         }
67                     }
68                     result.total = result.count = result.records.length;
69                     return Ext.create('Ext.data.ResultSet', result);
70                 },
71
72                 // private
73                 isEditableValue: function(val){
74                     return Ext.isPrimitive(val) || Ext.isDate(val);
75                 }
76             });
77         }
78         return this.reader;
79     },
80
81     // protected - should only be called by the grid.  Use grid.setSource instead.
82     setSource : function(dataObject) {
83         var me = this;
84
85         me.source = dataObject;
86         me.suspendEvents();
87         me.removeAll();
88         me.proxy.data = dataObject;
89         me.load();
90         me.resumeEvents();
91         me.fireEvent('datachanged', me);
92     },
93
94     // private
95     getProperty : function(row) {
96        return Ext.isNumber(row) ? this.store.getAt(row) : this.store.getById(row);
97     },
98
99     // private
100     setValue : function(prop, value, create){
101         var r = this.getRec(prop);
102         if (r) {
103             r.set('value', value);
104             this.source[prop] = value;
105         } else if (create) {
106             // only create if specified.
107             this.source[prop] = value;
108             r = new Ext.grid.property.Property({name: prop, value: value}, prop);
109             this.store.add(r);
110         }
111     },
112
113     // private
114     remove : function(prop) {
115         var r = this.getRec(prop);
116         if(r) {
117             this.store.remove(r);
118             delete this.source[prop];
119         }
120     },
121
122     // private
123     getRec : function(prop) {
124         return this.store.getById(prop);
125     },
126
127     // protected - should only be called by the grid.  Use grid.getSource instead.
128     getSource : function() {
129         return this.source;
130     }
131 });