3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.data.DataReader"></div>/**
16 * @class Ext.data.DataReader
17 * Abstract base class for reading structured data from a data source and converting
18 * it into an object containing {@link Ext.data.Record} objects and metadata for use
19 * by an {@link Ext.data.Store}. This class is intended to be extended and should not
20 * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
21 * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
22 * @constructor Create a new DataReader
23 * @param {Object} meta Metadata configuration options (implementation-specific).
24 * @param {Array/Object} recordType
25 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
26 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
27 * constructor created using {@link Ext.data.Record#create}.</p>
29 Ext.data.DataReader = function(meta, recordType){
30 <div id="prop-Ext.data.DataReader-meta"></div>/**
31 * This DataReader's configured metadata as passed to the constructor.
36 <div id="cfg-Ext.data.DataReader-fields"></div>/**
37 * @cfg {Array/Object} fields
38 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
39 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
40 * constructor created from {@link Ext.data.Record#create}.</p>
42 this.recordType = Ext.isArray(recordType) ?
43 Ext.data.Record.create(recordType) : recordType;
45 // if recordType defined make sure extraction functions are defined
47 this.buildExtractors();
51 Ext.data.DataReader.prototype = {
52 <div id="cfg-Ext.data.DataReader-messageProperty"></div>/**
53 * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
55 <div id="prop-Ext.data.DataReader-getTotal"></div>/**
56 * Abstract method created in extension's buildExtractors impl.
58 getTotal: Ext.emptyFn,
59 <div id="prop-Ext.data.DataReader-getRoot"></div>/**
60 * Abstract method created in extension's buildExtractors impl.
63 <div id="prop-Ext.data.DataReader-getMessage"></div>/**
64 * Abstract method created in extension's buildExtractors impl.
66 getMessage: Ext.emptyFn,
67 <div id="prop-Ext.data.DataReader-getSuccess"></div>/**
68 * Abstract method created in extension's buildExtractors impl.
70 getSuccess: Ext.emptyFn,
71 <div id="prop-Ext.data.DataReader-getId"></div>/**
72 * Abstract method created in extension's buildExtractors impl.
75 <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**
76 * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
78 buildExtractors : Ext.emptyFn,
79 <div id="prop-Ext.data.DataReader-extractValues"></div>/**
80 * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
82 extractValues : Ext.emptyFn,
84 <div id="method-Ext.data.DataReader-realize"></div>/**
85 * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
86 * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
87 * data from server will be merged with the data in the local record.
88 * In addition, you <b>must</b> return record-data from the server in the same order received.
89 * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
90 * @param {Record/Record[]} record The phantom record to be realized.
91 * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
93 realize: function(rs, data){
94 if (Ext.isArray(rs)) {
95 for (var i = rs.length - 1; i >= 0; i--) {
97 if (Ext.isArray(data)) {
98 this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
101 // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
102 // the else clause below will detect !this.isData and throw exception.
103 this.realize(rs.splice(i,1).shift(), data);
108 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
109 if (Ext.isArray(data) && data.length == 1) {
112 if (!this.isData(data)) {
113 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
115 throw new Ext.data.DataReader.Error('realize', rs);
117 rs.phantom = false; // <-- That's what it's all about
118 rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
119 rs.id = this.getId(data);
126 <div id="method-Ext.data.DataReader-update"></div>/**
127 * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
128 * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
129 * the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be
130 * suppressed as the record receives fresh new data-hash
131 * @param {Record/Record[]} rs
132 * @param {Object/Object[]} data
134 update : function(rs, data) {
135 if (Ext.isArray(rs)) {
136 for (var i=rs.length-1; i >= 0; i--) {
137 if (Ext.isArray(data)) {
138 this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
141 // weird...rs is an array but data isn't?? recurse but just send in the whole data object.
142 // the else clause below will detect !this.isData and throw exception.
143 this.update(rs.splice(i,1).shift(), data);
148 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
149 if (Ext.isArray(data) && data.length == 1) {
152 if (this.isData(data)) {
153 rs.data = Ext.apply(rs.data, data);
160 * returns extracted, type-cast rows of data. Iterates to call #extractValues for each row
161 * @param {Object[]/Object} data-root from server response
162 * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
165 extractData : function(root, returnRecords) {
166 // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
167 var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
171 // Had to add Check for XmlReader, #isData returns true if root is an Xml-object. Want to check in order to re-factor
172 // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
173 if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
176 var f = this.recordType.prototype.fields,
180 if (returnRecords === true) {
181 var Record = this.recordType;
182 for (var i = 0; i < root.length; i++) {
184 var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
185 record[rawName] = n; // <-- There's implementation of ugly bit, setting the raw record-data.
190 for (var i = 0; i < root.length; i++) {
191 var data = this.extractValues(root[i], fi, fl);
192 data[this.meta.idProperty] = this.getId(root[i]);
199 <div id="method-Ext.data.DataReader-isData"></div>/**
200 * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
201 * corresponding to idProperty defined in your DataReader config containing non-empty pk.
202 * @param {Object} data
205 isData : function(data) {
206 return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
209 // private function a store will createSequence upon
210 onMetaChange : function(meta){
213 this.recordType = Ext.data.Record.create(meta.fields);
214 this.buildExtractors();
218 <div id="cls-Ext.data.DataReader.Error"></div>/**
219 * @class Ext.data.DataReader.Error
221 * General error class for Ext.data.DataReader
223 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
224 constructor : function(message, arg) {
226 Ext.Error.call(this, message);
228 name: 'Ext.data.DataReader'
230 Ext.apply(Ext.data.DataReader.Error.prototype, {
232 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
233 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
234 'invalid-response': "#readResponse received an invalid response from the server."