Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / data / proxy / Memory.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  * @author Ed Spencer
17  *
18  * In-memory proxy. This proxy simply uses a local variable for data storage/retrieval, so its contents are lost on
19  * every page refresh.
20  *
21  * Usually this Proxy isn't used directly, serving instead as a helper to a {@link Ext.data.Store Store} where a reader
22  * is required to load data. For example, say we have a Store for a User model and have some inline data we want to
23  * load, but this data isn't in quite the right format: we can use a MemoryProxy with a JsonReader to read it into our
24  * Store:
25  *
26  *     //this is the model we will be using in the store
27  *     Ext.define('User', {
28  *         extend: 'Ext.data.Model',
29  *         fields: [
30  *             {name: 'id',    type: 'int'},
31  *             {name: 'name',  type: 'string'},
32  *             {name: 'phone', type: 'string', mapping: 'phoneNumber'}
33  *         ]
34  *     });
35  *
36  *     //this data does not line up to our model fields - the phone field is called phoneNumber
37  *     var data = {
38  *         users: [
39  *             {
40  *                 id: 1,
41  *                 name: 'Ed Spencer',
42  *                 phoneNumber: '555 1234'
43  *             },
44  *             {
45  *                 id: 2,
46  *                 name: 'Abe Elias',
47  *                 phoneNumber: '666 1234'
48  *             }
49  *         ]
50  *     };
51  *
52  *     //note how we set the 'root' in the reader to match the data structure above
53  *     var store = Ext.create('Ext.data.Store', {
54  *         autoLoad: true,
55  *         model: 'User',
56  *         data : data,
57  *         proxy: {
58  *             type: 'memory',
59  *             reader: {
60  *                 type: 'json',
61  *                 root: 'users'
62  *             }
63  *         }
64  *     });
65  */
66 Ext.define('Ext.data.proxy.Memory', {
67     extend: 'Ext.data.proxy.Client',
68     alias: 'proxy.memory',
69     alternateClassName: 'Ext.data.MemoryProxy',
70
71     /**
72      * @cfg {Ext.data.Model[]} data
73      * Optional array of Records to load into the Proxy
74      */
75
76     constructor: function(config) {
77         this.callParent([config]);
78
79         //ensures that the reader has been instantiated properly
80         this.setReader(this.reader);
81     },
82
83     /**
84      * Reads data from the configured {@link #data} object. Uses the Proxy's {@link #reader}, if present.
85      * @param {Ext.data.Operation} operation The read Operation
86      * @param {Function} callback The callback to call when reading has completed
87      * @param {Object} scope The scope to call the callback function in
88      */
89     read: function(operation, callback, scope) {
90         var me     = this,
91             reader = me.getReader(),
92             result = reader.read(me.data);
93
94         Ext.apply(operation, {
95             resultSet: result
96         });
97
98         operation.setCompleted();
99         operation.setSuccessful();
100         Ext.callback(callback, scope || me, [operation]);
101     },
102
103     clear: Ext.emptyFn
104 });
105