X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6..7a654f8d43fdb43d78b63d90528bed6e86b608cc:/src/data/reader/Array.js diff --git a/src/data/reader/Array.js b/src/data/reader/Array.js new file mode 100644 index 00000000..16f29aa4 --- /dev/null +++ b/src/data/reader/Array.js @@ -0,0 +1,66 @@ +/** + * @author Ed Spencer + * @class Ext.data.reader.Array + * @extends Ext.data.reader.Json + * + *

Data reader class to create an Array of {@link Ext.data.Model} objects from an Array. + * Each element of that Array represents a row of data fields. The + * fields are pulled into a Record object using as a subscript, the mapping property + * of the field definition if it exists, or the field's ordinal position in the definition.

+ * + *

Example code:

+ * +

+Employee = Ext.define('Employee', {
+    extend: 'Ext.data.Model',
+    fields: [
+        'id',
+        {name: 'name', mapping: 1},         // "mapping" only needed if an "id" field is present which
+        {name: 'occupation', mapping: 2}    // precludes using the ordinal position as the index.        
+    ]
+});
+
+var myReader = new Ext.data.reader.Array({
+    model: 'Employee'
+}, Employee);
+
+ * + *

This would consume an Array like this:

+ * +

+[ [1, 'Bill', 'Gardener'], [2, 'Ben', 'Horticulturalist'] ]
+
+ * + * @constructor + * Create a new ArrayReader + * @param {Object} meta Metadata configuration options. + */ +Ext.define('Ext.data.reader.Array', { + extend: 'Ext.data.reader.Json', + alternateClassName: 'Ext.data.ArrayReader', + alias : 'reader.array', + + /** + * @private + * Most of the work is done for us by JsonReader, but we need to overwrite the field accessors to just + * reference the correct position in the array. + */ + buildExtractors: function() { + this.callParent(arguments); + + var fields = this.model.prototype.fields.items, + length = fields.length, + extractorFunctions = [], + i; + + for (i = 0; i < length; i++) { + extractorFunctions.push(function(index) { + return function(data) { + return data[index]; + }; + }(fields[i].mapping || i)); + } + + this.extractorFunctions = extractorFunctions; + } +});