3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.DataReader
\r
9 * Abstract base class for reading structured data from a data source and converting
\r
10 * it into an object containing {@link Ext.data.Record} objects and metadata for use
\r
11 * by an {@link Ext.data.Store}. This class is intended to be extended and should not
\r
12 * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
\r
13 * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
\r
14 * @constructor Create a new DataReader
\r
15 * @param {Object} meta Metadata configuration options (implementation-specific).
\r
16 * @param {Array/Object} recordType
\r
17 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
18 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
19 * constructor created using {@link Ext.data.Record#create}.</p>
\r
21 Ext.data.DataReader = function(meta, recordType){
\r
23 * This DataReader's configured metadata as passed to the constructor.
\r
29 * @cfg {Array/Object} fields
\r
30 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
31 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
32 * constructor created from {@link Ext.data.Record#create}.</p>
\r
34 this.recordType = Ext.isArray(recordType) ?
\r
35 Ext.data.Record.create(recordType) : recordType;
\r
38 Ext.data.DataReader.prototype = {
\r
41 * Abstract method, overridden in {@link Ext.data.JsonReader}
\r
43 buildExtractors : Ext.emptyFn,
\r
46 * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
\r
47 * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
\r
48 * data from server will be merged with the data in the local record.
\r
49 * In addition, you <b>must</b> return record-data from the server in the same order received.
\r
50 * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
\r
51 * @param {Record/Record[]} record The phantom record to be realized.
\r
52 * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
\r
54 realize: function(rs, data){
\r
55 if (Ext.isArray(rs)) {
\r
56 for (var i = rs.length - 1; i >= 0; i--) {
\r
58 if (Ext.isArray(data)) {
\r
59 this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
\r
62 // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
\r
63 // the else clause below will detect !this.isData and throw exception.
\r
64 this.realize(rs.splice(i,1).shift(), data);
\r
69 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
\r
70 if (Ext.isArray(data) && data.length == 1) {
\r
71 data = data.shift();
\r
73 if (!this.isData(data)) {
\r
74 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
\r
76 throw new Ext.data.DataReader.Error('realize', rs);
\r
78 this.buildExtractors();
\r
79 var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);
\r
80 rs.phantom = false; // <-- That's what it's all about
\r
81 rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
\r
82 rs.id = data[this.meta.idProperty];
\r
89 * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
\r
90 * You <b>must</b> return a complete new record from the server. If you don't, your local record's missing fields
\r
91 * will be populated with the default values specified in your Ext.data.Record.create specification. Without a defaultValue,
\r
92 * local fields will be populated with empty string "". So return your entire record's data after both remote create and update.
\r
93 * In addition, you <b>must</b> return record-data from the server in the same order received.
\r
94 * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed as the record receives
\r
95 * a fresh new data-hash.
\r
96 * @param {Record/Record[]} rs
\r
97 * @param {Object/Object[]} data
\r
99 update : function(rs, data) {
\r
100 if (Ext.isArray(rs)) {
\r
101 for (var i=rs.length-1; i >= 0; i--) {
\r
102 if (Ext.isArray(data)) {
\r
103 this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
\r
106 // weird...rs is an array but data isn't?? recurse but just send in the whole data object.
\r
107 // the else clause below will detect !this.isData and throw exception.
\r
108 this.update(rs.splice(i,1).shift(), data);
\r
113 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
\r
114 if (Ext.isArray(data) && data.length == 1) {
\r
115 data = data.shift();
\r
117 if (!this.isData(data)) {
\r
118 // TODO: create custom Exception class to return record in thrown exception. Allow exception-handler the choice
\r
119 // to commit or not rather than blindly rs.commit() here.
\r
121 throw new Ext.data.DataReader.Error('update', rs);
\r
123 this.buildExtractors();
\r
124 rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);
\r
130 * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
\r
131 * corresponding to idProperty defined in your DataReader config containing non-empty pk.
\r
132 * @param {Object} data
\r
133 * @return {Boolean}
\r
135 isData : function(data) {
\r
136 return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;
\r
141 * @class Ext.data.DataReader.Error
\r
142 * @extends Ext.Error
\r
143 * General error class for Ext.data.DataReader
\r
145 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
\r
146 constructor : function(message, arg) {
\r
148 Ext.Error.call(this, message);
\r
150 name: 'Ext.data.DataReader'
\r
152 Ext.apply(Ext.data.DataReader.Error.prototype, {
\r
154 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
\r
155 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
\r
156 'invalid-response': "#readResponse received an invalid response from the server."
\r