3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('recordset-base', function(Y) {
10
* Provides a wrapper around a standard javascript object. Can be inserted into a Recordset instance.
14
var Record = Y.Base.create('record', Y.Base, [], {
19
initializer: function() {
22
destructor: function() {
26
* Retrieve a particular (or all) values from the object
28
* @param field {string} (optional) The key to retrieve the value from. If not supplied, the entire object is returned.
32
getValue: function(field) {
33
if (field === undefined) {
34
return this.get("data");
37
return this.get("data")[field];
46
* @description Unique ID of the record instance
55
* @description The object stored within the record instance
67
The Recordset utility provides a standard way for dealing with
68
a collection of similar objects.
71
@submodule recordset-base
75
var ArrayList = Y.ArrayList,
79
The Recordset utility provides a standard way for dealing with
80
a collection of similar objects.
82
Provides the base Recordset implementation, which can be extended to add
83
additional functionality, such as custom indexing. sorting, and filtering.
88
@param config {Object} Configuration object with initial attribute values
91
Recordset = Y.Base.create('recordset', Y.Base, [], {
95
* Publish default functions for events. Create the initial hash table.
100
initializer: function() {
101
// The reason the conditional is needed is because of two scenarios:
102
// 1. Instantiating new Y.Recordset() will not go into the setter of "records", and so it is necessary to create this._items in the initializer.
103
// 2. Instantiating new Y.Recordset({records: [{...}]}) will call the setter of "records" and create this._items. In this case, we don't want that to be overwritten by [].
108
//set up event listener to fire events when recordset is modified in anyway
111
* <p>At least one record is being added. Additional properties of
115
* <dd>Array of new records to be added</dd>
117
* <dd>The insertion index in the Recordset's internal
121
* <p>Preventing this event will cause the new records NOT to be
122
* added to the Recordset's internal collection.</p>
125
* @preventable _defAddFn
127
add: { defaultFn: this._defAddFn },
130
* <p>At least one record is being removed. Additional properties of
134
* <dd>Array of records to be removed</dd>
136
* <dd>Number of records to be removed</dd>
138
* <dd>The starting index in the Recordset's internal
139
* array from which to remove records</dd>
142
* <p>Preventing this event will cause the records NOT to be
143
* removed from the Recordset's internal collection.</p>
146
* @preventable _defRemoveFn
148
remove: { defaultFn: this._defRemoveFn },
151
* The Recordset is being flushed of all records.
154
* @preventable _defEmptyFn
156
empty: { defaultFn: this._defEmptyFn },
159
* <p>At least one record is being updated. Additional properties of
163
* <dd>Array of records with updated values</dd>
164
* <dt>overwritten</dt>
165
* <dd>Array of current records that will be replaced</dd>
167
* <dd>The starting index in the Recordset's internal
168
* array from which to update will apply</dd>
171
* <p>Preventing this event will cause the records NOT to be
172
* updated in the Recordset's internal collection.</p>
175
* @preventable _defUpdateFn
177
update: { defaultFn: this._defUpdateFn }
180
this._buildHashTable(this.get('key'));
187
'empty'], this._updateHash);
191
* Returns the record with particular ID or index
194
* @param i {String, Number} The ID of the record if a string, or the index if a number.
195
* @return {Record} A Y.Record instance
197
getRecord: function(i) {
199
if (Lang.isString(i)) {
200
return this.get('table')[i];
202
else if (Lang.isNumber(i)) {
203
return this._items[i];
210
* Returns the record at a particular index
212
* @method getRecordByIndex
213
* @param i {Number} Index at which the required record resides
214
* @return {Record} A Y.Record instance
216
getRecordByIndex: function(i) {
217
return this._items[i];
221
* Returns a range of records beginning at particular index
223
* @method getRecordsByIndex
224
* @param index {Number} Index at which the required record resides
225
* @param range {Number} (Optional) Number of records to retrieve. The default is 1
226
* @return {Array} An array of Y.Record instances
228
getRecordsByIndex: function(index, range) {
230
returnedRecords = [];
231
//Range cannot take on negative values
232
range = (Lang.isNumber(range) && (range > 0)) ? range: 1;
234
for (; i < range; i++) {
235
returnedRecords.push(this._items[index + i]);
237
return returnedRecords;
241
* Returns the length of the recordset
244
* @return {Number} Number of records in the recordset
246
getLength: function() {
251
Gets an array of values for a data _key_ in the set's records. If no _key_
252
is supplied, the returned array will contain the full data object for each
255
@method getValuesByKey
256
@param {String} [key] Data property to get from all records
257
@return {Array} An array of values for the given _key_ if supplied.
258
Otherwise, an array of each record's data hash.
260
getValuesByKey: function(key) {
262
len = this._items.length,
264
for (; i < len; i++) {
265
retVals.push(this._items[i].getValue(key));
272
* Adds one or more Records to the RecordSet at the given index. If index is null, then adds the Records to the end of the RecordSet.
275
* @param {Record|Object|Array} oData A Y.Record instance, An object literal of data or an array of object literals
276
* @param [index] {Number} [index] Index at which to add the record(s)
277
* @return {Recordset} The updated recordset instance
279
add: function(oData, index) {
285
idx = (Lang.isNumber(index) && (index > -1)) ? index: this._items.length;
287
//Passing in array of object literals for oData
288
if (Lang.isArray(oData)) {
289
for (; i < oData.length; i++) {
290
newRecords[i] = this._changeToRecord(oData[i]);
292
} else if (Lang.isObject(oData)) {
293
newRecords[0] = this._changeToRecord(oData);
304
Removes one or more Records to the RecordSet at the given index. If index
305
is null, then removes a single Record from the end of the RecordSet.
308
@param {Number} [index] Index at which to remove the record(s) from
309
@param {Number} [range] Number of records to remove (including the one
311
@return {Recordset} The updated recordset instance
313
remove: function(index, range) {
316
//Default is to only remove the last record - the length is always 1 greater than the last index
317
index = (index > -1) ? index: (this._items.length - 1);
318
range = (range > 0) ? range: 1;
320
remRecords = this._items.slice(index, (index + range));
321
this.fire('remove', {
326
//this._recordRemoved(remRecords, index);
327
//return ({data: remRecords, index:index});
332
* Empties the recordset
335
* @return {Recordset} The updated recordset instance
338
this.fire('empty', {});
343
Updates the recordset with the new records passed in. Overwrites existing
344
records when updating the index with the new records.
347
@param {Record|Object|Array} data A Y.Record instance, An object literal of
348
data or an array of object literals
349
@param {Number} [index] The index to start updating from.
350
@return {Recordset} The updated recordset instance
352
update: function(data, index) {
357
// Whatever is passed in, we are changing it to an array so that it can
358
// be easily iterated in the _defUpdateFn method
359
arr = (!(Lang.isArray(data))) ? [data] : data;
360
rec = this._items.slice(index, index + arr.length);
362
for (; i < arr.length; i++) {
363
arr[i] = this._changeToRecord(arr[i]);
366
this.fire('update', {
376
* Default behavior for the "add" event. Adds Record instances starting from
377
* the index specified in `e.index`.
380
* @param {EventFacade} e The add event
383
_defAddFn: function(e) {
384
this._items.splice.apply(this._items, [e.index, 0].concat(e.added));
388
* Default behavior for the "remove" event. Removes Records from the
389
* internal array starting from `e.index`. By default, it will remove one
390
* Record. But if `e.range` is set, it will remove that many Records.
392
* @method _defRemoveFn
393
* @param {EventFacade} e The remove event
396
_defRemoveFn: function(e) {
397
this._items.splice(e.index, e.range || 1);
401
* Default behavior for the "update" event. Sets Record instances for each
402
* item in `e.updated` at indexes starting from `e.index`.
404
* @method _defUpdateFn
405
* @param {EventFacade} e The update event
408
_defUpdateFn: function(e) {
409
for (var i = 0; i < e.updated.length; i++) {
410
this._items[e.index + i] = this._changeToRecord(e.updated[i]);
415
* Default behavior for the "empty" event. Clears the internal array of
418
* @method _defEmptyFn
419
* @param {EventFacade} e The empty event
422
_defEmptyFn: function(e) {
424
Y.log('empty fired');
428
Updates the internal hash table.
430
@method _defUpdateHash
431
@param {EventFacade} e Event triggering the hash table update
434
_updateHash: function (e) {
435
var handler = "_hash",
436
type = e.type.replace(/.*:/,''),
439
// _hashAdd, _hashRemove, _hashEmpty, etc
440
// Not a switch or else if setup to allow for external expansion.
441
handler += type.charAt(0).toUpperCase() + type.slice(1);
443
newHash = this[handler] &&
444
this[handler](this.get('table'), this.get('key'), e);
447
this.set('table', newHash);
452
Regenerates the hash table from the current internal array of Records.
454
@method _hashRecordsChange
455
@param {Object} hash The hash map before replacement
456
@param {String} key The key by which to add items to the hash
457
@param {Object} e The event or object containing the items to be added.
458
Items are expected to be stored in an array assigned to
459
the `added` property.
460
@return {Object} The updated hash map
463
_hashRecordsChange: function (hash, key, e) {
464
return this._buildHashTable(key);
468
Builds a hash table from the current internal array of Records.
470
@method _buildHashTable
471
@param {String} key The Record key to hash the items by
472
@return {Object} A new hash map of Records keyed by each Records' key
475
_buildHashTable: function (key) {
476
return this._hashAdd({}, key, { added: this._items });
480
Adds items to the hash table. Items are the values, and the keys are the
481
values of the item's attribute named in the `key` parameter.
484
@param {Object} hash The hash map before adding items
485
@param {String} key The key by which to add the items to the hash
486
@param {Object} e The event or object containing the items to be added.
487
Items are expected to be stored in an array assigned to
488
the `added` property.
489
@return {Object} The updated hash map
492
_hashAdd: function(hash, key, e) {
496
for (i = 0, len = e.added.length; i < len; ++i) {
497
hash[items[i].get(key)] = items[i];
504
Removes items from the hash table.
507
@param {Object} hash The hash map before removing items
508
@param {String} key The key by which to remove the items from the hash
509
@param {Object} e The event or object containing the items to be removed.
510
Items are expected to be stored in an array assigned to
511
the `removed` property.
512
@return {Object} The updated hash map
515
_hashRemove: function(hash, key, e) {
516
for (var i = e.removed.length - 1; i >= 0; --i) {
517
delete hash[e.removed[i].get(key)];
524
Updates items in the hash table.
527
@param {Object} hash The hash map before updating items
528
@param {String} key The key by which to update the items to the hash
529
@param {Object} e The event or object containing the items to be updated.
530
Items are expected to be stored in an array assigned to
531
the `updated` property. Optionally, items can be
532
identified for being overwritten by including them in an
533
array assigned to the `overwritten` property.
534
@return {Object} The updated hash map
537
_hashUpdate: function (hash, key, e) {
538
if (e.overwritten && e.overwritten.length) {
539
hash = this._hashRemove(hash, key, { removed: e.overwritten });
542
return this._hashAdd(hash, key, { added: e.updated });
546
Clears the hash table.
549
@param {Object} hash The hash map before adding items
550
@param {String} key The key by which to remove the items from the hash
551
@param {Object} e The event or object containing the items to be removed.
552
Items are expected to be stored in an array assigned to
553
the `removed` property.
554
@return {Object} An empty hash
557
_hashEmpty: function() {
562
* Sets up the hashtable with all the records currently in the recordset
564
* @method _initHashTable
567
_initHashTable: function() {
568
return this._hashAdd({}, this.get('key'), { added: this._items || [] });
572
* Helper method - it takes an object bag and converts it to a Y.Record
574
* @method _changeToRecord
575
* @param obj {Object|Record} Any objet literal or Y.Record instance
576
* @return {Record} A Record instance.
579
_changeToRecord: function(obj) {
580
return (obj instanceof Y.Record) ? obj : new Y.Record({ data: obj });
584
Ensures the value being set is an array of Record instances. If array items
585
are raw object data, they are turned into Records.
588
@param {Record[]|Object[]} items The Records or data Objects to store as
592
_setRecords: function (items) {
593
if (!Y.Lang.isArray(items)) {
594
return Y.Attribute.INVALID_VALUE;
600
// FIXME: This should use the flyweight pattern if possible
601
for (i = 0, len = items.length; i < len; ++i) {
602
records[i] = this._changeToRecord(items[i]);
605
return (this._items = records);
611
* An array of Records that the Recordset is storing. Passing an array
612
* of raw record data is also accepted. The data for each item will be
613
* wrapped in a Record instance.
619
// TODO: necessary? valueFn?
622
// give them a copy, not the internal object
623
return Y.Array(this._items);
625
setter: "_setRecords"
629
A hash table where the ID of the record is the key, and the record
630
instance is the value.
636
valueFn: '_initHashTable'
640
The ID to use as the key in the hash table.
652
Y.augment(Recordset, ArrayList);
653
Y.Recordset = Recordset;
657
}, '3.5.1' ,{requires:['base','arraylist']});