Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / data / MemoryProxy.js
1 /*!
2  * Ext JS Library 3.1.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**\r
8  * @class Ext.data.MemoryProxy\r
9  * @extends Ext.data.DataProxy\r
10  * An implementation of Ext.data.DataProxy that simply passes the data specified in its constructor\r
11  * to the Reader when its load method is called.\r
12  * @constructor\r
13  * @param {Object} data The data object which the Reader uses to construct a block of Ext.data.Records.\r
14  */\r
15 Ext.data.MemoryProxy = function(data){\r
16     // Must define a dummy api with "read" action to satisfy DataProxy#doRequest and Ext.data.Api#prepare *before* calling super\r
17     var api = {};\r
18     api[Ext.data.Api.actions.read] = true;\r
19     Ext.data.MemoryProxy.superclass.constructor.call(this, {\r
20         api: api\r
21     });\r
22     this.data = data;\r
23 };\r
24 \r
25 Ext.extend(Ext.data.MemoryProxy, Ext.data.DataProxy, {\r
26     /**\r
27      * @event loadexception\r
28      * Fires if an exception occurs in the Proxy during data loading. Note that this event is also relayed\r
29      * through {@link Ext.data.Store}, so you can listen for it directly on any Store instance.\r
30      * @param {Object} this\r
31      * @param {Object} arg The callback's arg object passed to the {@link #load} function\r
32      * @param {Object} null This parameter does not apply and will always be null for MemoryProxy\r
33      * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data\r
34      */\r
35 \r
36        /**\r
37      * MemoryProxy implementation of DataProxy#doRequest\r
38      * @param {String} action\r
39      * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null\r
40      * @param {Object} params An object containing properties which are to be used as HTTP parameters\r
41      * for the request to the remote server.\r
42      * @param {Ext.data.DataReader} reader The Reader object which converts the data\r
43      * object into a block of Ext.data.Records.\r
44      * @param {Function} callback The function into which to pass the block of Ext.data.Records.\r
45      * The function must be passed <ul>\r
46      * <li>The Record block object</li>\r
47      * <li>The "arg" argument from the load function</li>\r
48      * <li>A boolean success indicator</li>\r
49      * </ul>\r
50      * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.\r
51      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.\r
52      */\r
53     doRequest : function(action, rs, params, reader, callback, scope, arg) {\r
54         // No implementation for CRUD in MemoryProxy.  Assumes all actions are 'load'\r
55         params = params || {};\r
56         var result;\r
57         try {\r
58             result = reader.readRecords(this.data);\r
59         }catch(e){\r
60             // @deprecated loadexception\r
61             this.fireEvent("loadexception", this, null, arg, e);\r
62 \r
63             this.fireEvent('exception', this, 'response', action, arg, null, e);\r
64             callback.call(scope, null, arg, false);\r
65             return;\r
66         }\r
67         callback.call(scope, result, arg, true);\r
68     }\r
69 });