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