3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataReader"></div>/**
\r
9 * @class Ext.data.DataReader
\r
10 * Abstract base class for reading structured data from a data source and converting
\r
11 * it into an object containing {@link Ext.data.Record} objects and metadata for use
\r
12 * by an {@link Ext.data.Store}. This class is intended to be extended and should not
\r
13 * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
\r
14 * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
\r
15 * @constructor Create a new DataReader
\r
16 * @param {Object} meta Metadata configuration options (implementation-specific).
\r
17 * @param {Array/Object} recordType
\r
18 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
19 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
20 * constructor created using {@link Ext.data.Record#create}.</p>
\r
22 Ext.data.DataReader = function(meta, recordType){
\r
23 <div id="prop-Ext.data.DataReader-meta"></div>/**
\r
24 * This DataReader's configured metadata as passed to the constructor.
\r
29 <div id="cfg-Ext.data.DataReader-fields"></div>/**
\r
30 * @cfg {Array/Object} fields
\r
31 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
\r
32 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
\r
33 * constructor created from {@link Ext.data.Record#create}.</p>
\r
35 this.recordType = Ext.isArray(recordType) ?
\r
36 Ext.data.Record.create(recordType) : recordType;
\r
38 // make sure extraction functions are defined.
\r
39 this.buildExtractors();
\r
42 Ext.data.DataReader.prototype = {
\r
44 <div id="prop-Ext.data.DataReader-buildExtractors"></div>/**
\r
45 * Abstract method, overridden in {@link Ext.data.JsonReader}
\r
47 buildExtractors : Ext.emptyFn,
\r
49 <div id="method-Ext.data.DataReader-realize"></div>/**
\r
50 * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
\r
51 * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
\r
52 * data from server will be merged with the data in the local record.
\r
53 * In addition, you <b>must</b> return record-data from the server in the same order received.
\r
54 * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
\r
55 * @param {Record/Record[]} record The phantom record to be realized.
\r
56 * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
\r
58 realize: function(rs, data){
\r
59 if (Ext.isArray(rs)) {
\r
60 for (var i = rs.length - 1; i >= 0; i--) {
\r
62 if (Ext.isArray(data)) {
\r
63 this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
\r
66 // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
\r
67 // the else clause below will detect !this.isData and throw exception.
\r
68 this.realize(rs.splice(i,1).shift(), data);
\r
73 // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
\r
74 if (Ext.isArray(data) && data.length == 1) {
\r
75 data = data.shift();
\r
77 if (!this.isData(data)) {
\r
78 // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
\r
80 throw new Ext.data.DataReader.Error('realize', rs);
\r
82 var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);
\r
83 rs.phantom = false; // <-- That's what it's all about
\r
84 rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
\r
85 rs.id = data[this.meta.idProperty];
\r
91 <div id="method-Ext.data.DataReader-update"></div>/**
\r
92 * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
\r
93 * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
\r
94 * the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be
\r
95 * suppressed as the record receives 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 rs.data = this.extractValues(Ext.apply(rs.data, data), rs.fields.items, rs.fields.items.length);
\r
124 <div id="method-Ext.data.DataReader-isData"></div>/**
\r
125 * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
\r
126 * corresponding to idProperty defined in your DataReader config containing non-empty pk.
\r
127 * @param {Object} data
\r
128 * @return {Boolean}
\r
130 isData : function(data) {
\r
131 return (data && Ext.isObject(data) && !Ext.isEmpty(data[this.meta.idProperty])) ? true : false;
\r
135 <div id="cls-Ext.data.DataReader.Error"></div>/**
\r
136 * @class Ext.data.DataReader.Error
\r
137 * @extends Ext.Error
\r
138 * General error class for Ext.data.DataReader
\r
140 Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
\r
141 constructor : function(message, arg) {
\r
143 Ext.Error.call(this, message);
\r
145 name: 'Ext.data.DataReader'
\r
147 Ext.apply(Ext.data.DataReader.Error.prototype, {
\r
149 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
\r
150 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
\r
151 'invalid-response': "#readResponse received an invalid response from the server."
\r