Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / data / reader / Array.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.reader.Array
18  * @extends Ext.data.reader.Json
19  * 
20  * <p>Data reader class to create an Array of {@link Ext.data.Model} objects from an Array.
21  * Each element of that Array represents a row of data fields. The
22  * fields are pulled into a Record object using as a subscript, the <code>mapping</code> property
23  * of the field definition if it exists, or the field's ordinal position in the definition.</p>
24  * 
25  * <p><u>Example code:</u></p>
26  * 
27 <pre><code>
28 Employee = Ext.define('Employee', {
29     extend: 'Ext.data.Model',
30     fields: [
31         'id',
32         {name: 'name', mapping: 1},         // "mapping" only needed if an "id" field is present which
33         {name: 'occupation', mapping: 2}    // precludes using the ordinal position as the index.        
34     ]
35 });
36
37 var myReader = new Ext.data.reader.Array({
38     model: 'Employee'
39 }, Employee);
40 </code></pre>
41  * 
42  * <p>This would consume an Array like this:</p>
43  * 
44 <pre><code>
45 [ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
46 </code></pre>
47  * 
48  * @constructor
49  * Create a new ArrayReader
50  * @param {Object} meta Metadata configuration options.
51  */
52 Ext.define('Ext.data.reader.Array', {
53     extend: 'Ext.data.reader.Json',
54     alternateClassName: 'Ext.data.ArrayReader',
55     alias : 'reader.array',
56
57     /**
58      * @private
59      * Most of the work is done for us by JsonReader, but we need to overwrite the field accessors to just
60      * reference the correct position in the array.
61      */
62     buildExtractors: function() {
63         this.callParent(arguments);
64         
65         var fields = this.model.prototype.fields.items,
66             i = 0,
67             length = fields.length,
68             extractorFunctions = [],
69             map;
70         
71         for (; i < length; i++) {
72             map = fields[i].mapping;
73             extractorFunctions.push(function(index) {
74                 return function(data) {
75                     return data[index];
76                 };
77             }(map !== null ? map : i));
78         }
79         
80         this.extractorFunctions = extractorFunctions;
81     }
82 });
83