3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataReader"></div>/**
\r
10 * @class Ext.data.DataReader
\r
11 * Abstract base class for reading structured data from a data source and converting
\r
12 * it into an object containing {@link Ext.data.Record} objects and metadata for use
\r
13 * by an {@link Ext.data.Store}. This class is intended to be extended and should not
\r
14 * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
\r
15 * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
\r
16 * @constructor Create a new DataReader
\r
17 * @param {Object} meta Metadata configuration options (implementation-specific).
\r
18 * @param {Array/Object} recordType
\r
19 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
20 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
21 * constructor created using {@link Ext.data.Record#create}.</p>
\r
23 Ext.data.DataReader = function(meta, recordType){
\r
24 <div id="prop-Ext.data.DataReader-meta"></div>/**
\r
25 * This DataReader's configured metadata as passed to the constructor.
\r
30 <div id="cfg-Ext.data.DataReader-fields"></div>/**
\r
31 * @cfg {Array/Object} fields
\r
32 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
33 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
34 * constructor created from {@link Ext.data.Record#create}.</p>
\r
36 this.recordType = Ext.isArray(recordType) ?
\r
37 Ext.data.Record.create(recordType) : recordType;
\r
39 // if recordType defined make sure extraction functions are defined
\r
40 if (this.recordType){
\r
41 this.buildExtractors();
\r
45 Ext.data.DataReader.prototype = {
\r
46 <div id="cfg-Ext.data.DataReader-messageProperty"></div>/**
\r
47 * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
\r
49 <div id="prop-Ext.data.DataReader-getTotal"></div>/**
\r
50 * Abstract method created in extension's buildExtractors impl.
\r
52 getTotal: Ext.emptyFn,
\r
53 <div id="prop-Ext.data.DataReader-getRoot"></div>/**
\r
54 * Abstract method created in extension's buildExtractors impl.
\r
56 getRoot: Ext.emptyFn,
\r
57 <div id="prop-Ext.data.DataReader-getMessage"></div>/**
\r
58 * Abstract method created in extension's buildExtractors impl.
\r
60 getMessage: Ext.emptyFn,
\r
61 <div id="prop-Ext.data.DataReader-getSuccess"></div>/**
\r
62 * Abstract method created in extension's buildExtractors impl.
\r
64 getSuccess: Ext.emptyFn,
\r
65 <div id="prop-Ext.data.DataReader-getId"></div>/**
\r
66 * Abstract method created in extension's buildExtractors impl.
\r
69 <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**
\r
70 * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
\r
72 buildExtractors : Ext.emptyFn,
\r
73 <div id="prop-Ext.data.DataReader-extractData"></div>/**
\r
74 * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
\r
76 extractData : Ext.emptyFn,
\r
77 <div id="prop-Ext.data.DataReader-extractValues"></div>/**
\r
78 * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
\r
80 extractValues : Ext.emptyFn,
\r
82 <div id="method-Ext.data.DataReader-realize"></div>/**
\r
83 * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
\r
84 * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
\r
85 * data from server will be merged with the data in the local record.
\r
86 * In addition, you <b>must</b> return record-data from the server in the same order received.
\r
87 * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
\r
88 * @param {Record/Record[]} record The phantom record to be realized.
\r
89 * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
\r
91 realize: function(rs, data){
\r
92 if (Ext.isArray(rs)) {
\r
93 for (var i = rs.length - 1; i >= 0; i--) {
\r
95 if (Ext.isArray(data)) {
\r
96 this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
\r
99 // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
\r
100 // the else clause below will detect !this.isData and throw exception.
\r
101 this.realize(rs.splice(i,1).shift(), data);
\r
106 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
\r
107 if (Ext.isArray(data) && data.length == 1) {
\r
108 data = data.shift();
\r
110 if (!this.isData(data)) {
\r
111 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
\r
113 throw new Ext.data.DataReader.Error('realize', rs);
\r
115 rs.phantom = false; // <-- That's what it's all about
\r
116 rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
\r
117 rs.id = this.getId(data);
\r
119 rs.fields.each(function(f) {
\r
120 if (data[f.name] !== f.defaultValue) {
\r
121 rs.data[f.name] = data[f.name];
\r
128 <div id="method-Ext.data.DataReader-update"></div>/**
\r
129 * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
\r
130 * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
\r
131 * the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be
\r
132 * suppressed as the record receives fresh new data-hash
\r
133 * @param {Record/Record[]} rs
\r
134 * @param {Object/Object[]} data
\r
136 update : function(rs, data) {
\r
137 if (Ext.isArray(rs)) {
\r
138 for (var i=rs.length-1; i >= 0; i--) {
\r
139 if (Ext.isArray(data)) {
\r
140 this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
\r
143 // weird...rs is an array but data isn't?? recurse but just send in the whole data object.
\r
144 // the else clause below will detect !this.isData and throw exception.
\r
145 this.update(rs.splice(i,1).shift(), data);
\r
150 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
\r
151 if (Ext.isArray(data) && data.length == 1) {
\r
152 data = data.shift();
\r
154 if (this.isData(data)) {
\r
155 rs.fields.each(function(f) {
\r
156 if (data[f.name] !== f.defaultValue) {
\r
157 rs.data[f.name] = data[f.name];
\r
166 * returns extracted, type-cast rows of data. Iterates to call #extractValues for each row
\r
167 * @param {Object[]/Object} data-root from server response
\r
168 * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
\r
171 extractData : function(root, returnRecords) {
\r
172 // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
\r
173 var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
\r
177 // Had to add Check for XmlReader, #isData returns true if root is an Xml-object. Want to check in order to re-factor
\r
178 // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
\r
179 if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
\r
182 var f = this.recordType.prototype.fields,
\r
186 if (returnRecords === true) {
\r
187 var Record = this.recordType;
\r
188 for (var i = 0; i < root.length; i++) {
\r
190 var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
\r
191 record[rawName] = n; // <-- There's implementation of ugly bit, setting the raw record-data.
\r
196 for (var i = 0; i < root.length; i++) {
\r
197 var data = this.extractValues(root[i], fi, fl);
\r
198 data[this.meta.idProperty] = this.getId(root[i]);
\r
205 <div id="method-Ext.data.DataReader-isData"></div>/**
\r
206 * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
\r
207 * corresponding to idProperty defined in your DataReader config containing non-empty pk.
\r
208 * @param {Object} data
\r
209 * @return {Boolean}
\r
211 isData : function(data) {
\r
212 return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
\r
215 // private function a store will createSequence upon
\r
216 onMetaChange : function(meta){
\r
219 this.recordType = Ext.data.Record.create(meta.fields);
\r
220 this.buildExtractors();
\r
224 <div id="cls-Ext.data.DataReader.Error"></div>/**
\r
225 * @class Ext.data.DataReader.Error
\r
226 * @extends Ext.Error
\r
227 * General error class for Ext.data.DataReader
\r
229 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
\r
230 constructor : function(message, arg) {
\r
232 Ext.Error.call(this, message);
\r
234 name: 'Ext.data.DataReader'
\r
236 Ext.apply(Ext.data.DataReader.Error.prototype, {
\r
238 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
\r
239 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
\r
240 'invalid-response': "#readResponse received an invalid response from the server."
\r