27 |
jpm |
1 |
/*
|
|
|
2 |
* Ext JS Library 2.0.2
|
|
|
3 |
* Copyright(c) 2006-2008, Ext JS, LLC.
|
|
|
4 |
* licensing@extjs.com
|
|
|
5 |
*
|
|
|
6 |
* http://extjs.com/license
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* @class Ext.data.Record
|
|
|
11 |
* Instances of this class encapsulate both record <em>definition</em> information, and record
|
|
|
12 |
* <em>value</em> information for use in {@link Ext.data.Store} objects, or any code which needs
|
|
|
13 |
* to access Records cached in an {@link Ext.data.Store} object.<br>
|
|
|
14 |
* <p>
|
|
|
15 |
* Constructors for this class are generated by passing an Array of field definition objects to {@link #create}.
|
|
|
16 |
* Instances are usually only created by {@link Ext.data.Reader} implementations when processing unformatted data
|
|
|
17 |
* objects.<br>
|
|
|
18 |
* <p>
|
|
|
19 |
* Record objects generated by this constructor inherit all the methods of Ext.data.Record listed below.
|
|
|
20 |
* @constructor
|
|
|
21 |
* This constructor should not be used to create Record objects. Instead, use the constructor generated by
|
|
|
22 |
* {@link #create}. The parameters are the same.
|
|
|
23 |
* @param {Array} data An associative Array of data values keyed by the field name.
|
|
|
24 |
* @param {Object} id (Optional) The id of the record. This id should be unique, and is used by the
|
|
|
25 |
* {@link Ext.data.Store} object which owns the Record to index its collection of Records. If
|
|
|
26 |
* not specified an integer id is generated.
|
|
|
27 |
*/
|
|
|
28 |
Ext.data.Record = function(data, id){
|
|
|
29 |
this.id = (id || id === 0) ? id : ++Ext.data.Record.AUTO_ID;
|
|
|
30 |
this.data = data;
|
|
|
31 |
};
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Generate a constructor for a specific record layout.
|
|
|
35 |
* @param {Array} o An Array of field definition objects which specify field names, and optionally,
|
|
|
36 |
* data types, and a mapping for an {@link Ext.data.Reader} to extract the field's value from a data object.
|
|
|
37 |
* Each field definition object may contain the following properties: <ul>
|
|
|
38 |
* <li><b>name</b> : String<p style="margin-left:1em">The name by which the field is referenced within the Record. This is referenced by,
|
|
|
39 |
* for example the <em>dataIndex</em> property in column definition objects passed to {@link Ext.grid.ColumnModel}</p></li>
|
|
|
40 |
* <li><b>mapping</b> : String<p style="margin-left:1em">(Optional) A path specification for use by the {@link Ext.data.Reader} implementation
|
|
|
41 |
* that is creating the Record to access the data value from the data object. If an {@link Ext.data.JsonReader}
|
|
|
42 |
* is being used, then this is a string containing the javascript expression to reference the data relative to
|
|
|
43 |
* the record item's root. If an {@link Ext.data.XmlReader} is being used, this is an {@link Ext.DomQuery} path
|
|
|
44 |
* to the data item relative to the record element. If the mapping expression is the same as the field name,
|
|
|
45 |
* this may be omitted.</p></li>
|
|
|
46 |
* <li><b>type</b> : String<p style="margin-left:1em">(Optional) The data type for conversion to displayable value. Possible values are
|
|
|
47 |
* <ul><li>auto (Default, implies no conversion)</li>
|
|
|
48 |
* <li>string</li>
|
|
|
49 |
* <li>int</li>
|
|
|
50 |
* <li>float</li>
|
|
|
51 |
* <li>boolean</li>
|
|
|
52 |
* <li>date</li></ul></p></li>
|
|
|
53 |
* <li><b>sortType</b> : Mixed<p style="margin-left:1em">(Optional) A member of {@link Ext.data.SortTypes}.</p></li>
|
|
|
54 |
* <li><b>sortDir</b> : String<p style="margin-left:1em">(Optional) Initial direction to sort. "ASC" or "DESC"</p></li>
|
|
|
55 |
* <li><b>convert</b> : Function<p style="margin-left:1em">(Optional) A function which converts the value provided
|
|
|
56 |
* by the Reader into an object that will be stored in the Record. It is passed the
|
|
|
57 |
* following parameters:<ul>
|
|
|
58 |
* <li><b>v</b> : Mixed<p style="margin-left:1em">The data value as read by the Reader.</p></li>
|
|
|
59 |
* <li><b>rec</b> : Mixed<p style="margin-left:1em">The data object containting the row as read by the Reader.
|
|
|
60 |
* Depending on Reader type, this could be an Array, an object, or an XML element.</p></li>
|
|
|
61 |
* </ul></p></li>
|
|
|
62 |
* <li><b>dateFormat</b> : String<p style="margin-left:1em">(Optional) A format String for the Date.parseDate function.</p></li>
|
|
|
63 |
* </ul>
|
|
|
64 |
* <br>usage:<br><pre><code>
|
|
|
65 |
var TopicRecord = Ext.data.Record.create([
|
|
|
66 |
{name: 'title', mapping: 'topic_title'},
|
|
|
67 |
{name: 'author', mapping: 'username'},
|
|
|
68 |
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
|
|
|
69 |
{name: 'lastPost', mapping: 'post_time', type: 'date'},
|
|
|
70 |
{name: 'lastPoster', mapping: 'user2'},
|
|
|
71 |
{name: 'excerpt', mapping: 'post_text'}
|
|
|
72 |
]);
|
|
|
73 |
|
|
|
74 |
var myNewRecord = new TopicRecord({
|
|
|
75 |
title: 'Do my job please',
|
|
|
76 |
author: 'noobie',
|
|
|
77 |
totalPosts: 1,
|
|
|
78 |
lastPost: new Date(),
|
|
|
79 |
lastPoster: 'Animal',
|
|
|
80 |
excerpt: 'No way dude!'
|
|
|
81 |
});
|
|
|
82 |
myStore.add(myNewRecord);
|
|
|
83 |
</code></pre>
|
|
|
84 |
* <p>In the simplest case, if no properties other than <tt>name</tt> are required, a field definition
|
|
|
85 |
* may consist of just a field name string.</p>
|
|
|
86 |
* @method create
|
|
|
87 |
* @return {function} A constructor which is used to create new Records according
|
|
|
88 |
* to the definition.
|
|
|
89 |
* @static
|
|
|
90 |
*/
|
|
|
91 |
Ext.data.Record.create = function(o){
|
|
|
92 |
var f = Ext.extend(Ext.data.Record, {});
|
|
|
93 |
var p = f.prototype;
|
|
|
94 |
p.fields = new Ext.util.MixedCollection(false, function(field){
|
|
|
95 |
return field.name;
|
|
|
96 |
});
|
|
|
97 |
for(var i = 0, len = o.length; i < len; i++){
|
|
|
98 |
p.fields.add(new Ext.data.Field(o[i]));
|
|
|
99 |
}
|
|
|
100 |
f.getField = function(name){
|
|
|
101 |
return p.fields.get(name);
|
|
|
102 |
};
|
|
|
103 |
return f;
|
|
|
104 |
};
|
|
|
105 |
|
|
|
106 |
Ext.data.Record.AUTO_ID = 1000;
|
|
|
107 |
Ext.data.Record.EDIT = 'edit';
|
|
|
108 |
Ext.data.Record.REJECT = 'reject';
|
|
|
109 |
Ext.data.Record.COMMIT = 'commit';
|
|
|
110 |
|
|
|
111 |
Ext.data.Record.prototype = {
|
|
|
112 |
/**
|
|
|
113 |
* The data for this record an object hash.
|
|
|
114 |
* @property data
|
|
|
115 |
* @type {Object}
|
|
|
116 |
*/
|
|
|
117 |
/**
|
|
|
118 |
* The unique ID of the record as specified at construction time.
|
|
|
119 |
* @property id
|
|
|
120 |
* @type {Object}
|
|
|
121 |
*/
|
|
|
122 |
/**
|
|
|
123 |
* Readonly flag - true if this record has been modified.
|
|
|
124 |
* @type Boolean
|
|
|
125 |
*/
|
|
|
126 |
dirty : false,
|
|
|
127 |
editing : false,
|
|
|
128 |
error: null,
|
|
|
129 |
/**
|
|
|
130 |
* This object contains a key and value storing the original values of all modified fields or is null if no fields have been modified.
|
|
|
131 |
* @property modified
|
|
|
132 |
* @type {Object}
|
|
|
133 |
*/
|
|
|
134 |
modified: null,
|
|
|
135 |
|
|
|
136 |
// private
|
|
|
137 |
join : function(store){
|
|
|
138 |
this.store = store;
|
|
|
139 |
},
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Set the named field to the specified value.
|
|
|
143 |
* @param {String} name The name of the field to set.
|
|
|
144 |
* @param {Object} value The value to set the field to.
|
|
|
145 |
*/
|
|
|
146 |
set : function(name, value){
|
|
|
147 |
if(String(this.data[name]) == String(value)){
|
|
|
148 |
return;
|
|
|
149 |
}
|
|
|
150 |
this.dirty = true;
|
|
|
151 |
if(!this.modified){
|
|
|
152 |
this.modified = {};
|
|
|
153 |
}
|
|
|
154 |
if(typeof this.modified[name] == 'undefined'){
|
|
|
155 |
this.modified[name] = this.data[name];
|
|
|
156 |
}
|
|
|
157 |
this.data[name] = value;
|
|
|
158 |
if(!this.editing && this.store){
|
|
|
159 |
this.store.afterEdit(this);
|
|
|
160 |
}
|
|
|
161 |
},
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Get the value of the named field.
|
|
|
165 |
* @param {String} name The name of the field to get the value of.
|
|
|
166 |
* @return {Object} The value of the field.
|
|
|
167 |
*/
|
|
|
168 |
get : function(name){
|
|
|
169 |
return this.data[name];
|
|
|
170 |
},
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Begin an edit. While in edit mode, no events are relayed to the containing store.
|
|
|
174 |
*/
|
|
|
175 |
beginEdit : function(){
|
|
|
176 |
this.editing = true;
|
|
|
177 |
this.modified = {};
|
|
|
178 |
},
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Cancels all changes made in the current edit operation.
|
|
|
182 |
*/
|
|
|
183 |
cancelEdit : function(){
|
|
|
184 |
this.editing = false;
|
|
|
185 |
delete this.modified;
|
|
|
186 |
},
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* End an edit. If any data was modified, the containing store is notified.
|
|
|
190 |
*/
|
|
|
191 |
endEdit : function(){
|
|
|
192 |
this.editing = false;
|
|
|
193 |
if(this.dirty && this.store){
|
|
|
194 |
this.store.afterEdit(this);
|
|
|
195 |
}
|
|
|
196 |
},
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Usually called by the {@link Ext.data.Store} which owns the Record.
|
|
|
200 |
* Rejects all changes made to the Record since either creation, or the last commit operation.
|
|
|
201 |
* Modified fields are reverted to their original values.
|
|
|
202 |
* <p>
|
|
|
203 |
* Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified
|
|
|
204 |
* of reject operations.
|
|
|
205 |
* @param {Boolean} silent (optional) True to skip notification of the owning store of the change (defaults to false)
|
|
|
206 |
*/
|
|
|
207 |
reject : function(silent){
|
|
|
208 |
var m = this.modified;
|
|
|
209 |
for(var n in m){
|
|
|
210 |
if(typeof m[n] != "function"){
|
|
|
211 |
this.data[n] = m[n];
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
this.dirty = false;
|
|
|
215 |
delete this.modified;
|
|
|
216 |
this.editing = false;
|
|
|
217 |
if(this.store && silent !== true){
|
|
|
218 |
this.store.afterReject(this);
|
|
|
219 |
}
|
|
|
220 |
},
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Usually called by the {@link Ext.data.Store} which owns the Record.
|
|
|
224 |
* Commits all changes made to the Record since either creation, or the last commit operation.
|
|
|
225 |
* <p>
|
|
|
226 |
* Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified
|
|
|
227 |
* of commit operations.
|
|
|
228 |
* @param {Boolean} silent (optional) True to skip notification of the owning store of the change (defaults to false)
|
|
|
229 |
*/
|
|
|
230 |
commit : function(silent){
|
|
|
231 |
this.dirty = false;
|
|
|
232 |
delete this.modified;
|
|
|
233 |
this.editing = false;
|
|
|
234 |
if(this.store && silent !== true){
|
|
|
235 |
this.store.afterCommit(this);
|
|
|
236 |
}
|
|
|
237 |
},
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Gets a hash of only the fields that have been modified since this record was created or commited.
|
|
|
241 |
* @return Object
|
|
|
242 |
*/
|
|
|
243 |
getChanges : function(){
|
|
|
244 |
var m = this.modified, cs = {};
|
|
|
245 |
for(var n in m){
|
|
|
246 |
if(m.hasOwnProperty(n)){
|
|
|
247 |
cs[n] = this.data[n];
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
return cs;
|
|
|
251 |
},
|
|
|
252 |
|
|
|
253 |
// private
|
|
|
254 |
hasError : function(){
|
|
|
255 |
return this.error != null;
|
|
|
256 |
},
|
|
|
257 |
|
|
|
258 |
// private
|
|
|
259 |
clearError : function(){
|
|
|
260 |
this.error = null;
|
|
|
261 |
},
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Creates a copy of this record.
|
|
|
265 |
* @param {String} id (optional) A new record id if you don't want to use this record's id
|
|
|
266 |
* @return {Record}
|
|
|
267 |
*/
|
|
|
268 |
copy : function(newId) {
|
|
|
269 |
return new this.constructor(Ext.apply({}, this.data), newId || this.id);
|
|
|
270 |
},
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Returns true if the field passed has been modified since the load or last commit.
|
|
|
274 |
* @param {String} fieldName
|
|
|
275 |
* @return {Boolean}
|
|
|
276 |
*/
|
|
|
277 |
isModified : function(fieldName){
|
|
|
278 |
return this.modified && this.modified.hasOwnProperty(fieldName);
|
|
|
279 |
}
|
|
|
280 |
};
|