Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Memory.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.proxy.Memory'>/**
2 </span> * @author Ed Spencer
3  * @class Ext.data.proxy.Memory
4  * @extends Ext.data.proxy.Client
5  *
6  * &lt;p&gt;In-memory proxy. This proxy simply uses a local variable for data storage/retrieval, so its contents are lost on
7  * every page refresh.&lt;/p&gt;
8  *
9  * &lt;p&gt;Usually this Proxy isn't used directly, serving instead as a helper to a {@link Ext.data.Store Store} where a
10  * reader is required to load data. For example, say we have a Store for a User model and have some inline data we want
11  * to load, but this data isn't in quite the right format: we can use a MemoryProxy with a JsonReader to read it into
12  * our Store:&lt;/p&gt;
13  *
14 &lt;pre&gt;&lt;code&gt;
15 //this is the model we will be using in the store
16 Ext.define('User', {
17     extend: 'Ext.data.Model',
18     fields: [
19         {name: 'id',    type: 'int'},
20         {name: 'name',  type: 'string'},
21         {name: 'phone', type: 'string', mapping: 'phoneNumber'}
22     ]
23 });
24
25 //this data does not line up to our model fields - the phone field is called phoneNumber
26 var data = {
27     users: [
28         {
29             id: 1,
30             name: 'Ed Spencer',
31             phoneNumber: '555 1234'
32         },
33         {
34             id: 2,
35             name: 'Abe Elias',
36             phoneNumber: '666 1234'
37         }
38     ]
39 };
40
41 //note how we set the 'root' in the reader to match the data structure above
42 var store = new Ext.data.Store({
43     autoLoad: true,
44     model: 'User',
45     data : data,
46     proxy: {
47         type: 'memory',
48         reader: {
49             type: 'json',
50             root: 'users'
51         }
52     }
53 });
54 &lt;/code&gt;&lt;/pre&gt;
55  */
56 Ext.define('Ext.data.proxy.Memory', {
57     extend: 'Ext.data.proxy.Client',
58     alias: 'proxy.memory',
59     alternateClassName: 'Ext.data.MemoryProxy',
60
61 <span id='Ext-data.proxy.Memory-cfg-data'>    /**
62 </span>     * @cfg {Array} data Optional array of Records to load into the Proxy
63      */
64
65     constructor: function(config) {
66         this.callParent([config]);
67
68         //ensures that the reader has been instantiated properly
69         this.setReader(this.reader);
70     },
71
72 <span id='Ext-data.proxy.Memory-method-read'>    /**
73 </span>     * Reads data from the configured {@link #data} object. Uses the Proxy's {@link #reader}, if present
74      * @param {Ext.data.Operation} operation The read Operation
75      * @param {Function} callback The callback to call when reading has completed
76      * @param {Object} scope The scope to call the callback function in
77      */
78     read: function(operation, callback, scope) {
79         var me     = this,
80             reader = me.getReader(),
81             result = reader.read(me.data);
82
83         Ext.apply(operation, {
84             resultSet: result
85         });
86
87         operation.setCompleted();
88         operation.setSuccessful();
89         Ext.callback(callback, scope || me, [operation]);
90     },
91
92     clear: Ext.emptyFn
93 });
94 </pre></pre></body></html>