recordset-base.js revision 67d8d00b3c046754ca1781560f46e582af3fd0d6
/**
* The Recordset utility provides a standard way for dealing with
* a collection of similar objects.
* @module recordset
* @submodule recordset-base
*/
/**
* The Recordset utility provides a standard way for dealing with
* a collection of similar objects.
*
* Provides the base Recordset implementation, which can be extended to add
* additional functionality, such as custom indexing. sorting, and filtering.
*
* @class Recordset
* @extends Base
* @augments ArrayList
* @param config {Object} Configuration object literal with initial attribute values
* @constructor
*/
/**
* @description Publish default functions for events. Create the initial hash table.
*
* @method initializer
* @protected
*/
initializer: function() {
/*
If this._items does not exist, then create it and set it to an empty array.
The reason the conditional is needed is because of two scenarios:
Instantiating new Y.Recordset() will not go into the setter of "records", and so
it is necessary to create this._items in the initializer.
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 [].
*/
if (!this._items) {
this._items = [];
}
//set up event listener to fire events when recordset is modified in anyway
this.publish('add', {
});
this.publish('remove', {
defaultFn: this._defRemoveFn
});
this.publish('empty', {
defaultFn: this._defEmptyFn
});
this.publish('update', {
defaultFn: this._defUpdateFn
});
this._recordsetChanged();
//Fires recordset changed event when any updates are made to the recordset
this._syncHashTable();
//Fires appropriate hashTable methods on "add", "remove", "update" and "empty" events
},
destructor: function() {
},
/**
* @description Helper method called upon by add() - it is used to create a new record(s) in the recordset
*
* @method _defAddFn
* @return {Y.Record} A Record instance.
* @private
*/
_defAddFn: function(e) {
i = 0;
//index = (Lang.isNumber(index) && (index > -1)) ? index : len;
//if records are to be added one at a time, push them in one at a time
}
else {
index++;
}
}
Y.log('add Fired');
},
/**
* @description Helper method called upon by remove() - it is used to remove record(s) from the recordset
*
* @method _defRemoveFn
* @private
*/
_defRemoveFn: function(e) {
//remove from beginning
if (e.index === 0) {
}
//remove from end
}
//remove from middle
else {
}
},
/**
* Helper method called upon by empty() - it is used to empty the recordset
*
* @method _defEmptyFn
* @private
*/
_defEmptyFn: function(e) {
this._items = [];
Y.log('empty fired');
},
/**
* @description Helper method called upon by update() - it is used to update the recordset
*
* @method _defUpdateFn
* @private
*/
_defUpdateFn: function(e) {
}
},
//---------------------------------------------
// Hash Table Methods
//---------------------------------------------
/**
* @description Method called whenever "recordset:add" event is fired. It adds the new record(s) to the hashtable.
*
* @method _defAddHash
* @private
*/
_defAddHash: function(e) {
i = 0;
}
},
/**
* @description Method called whenever "recordset:remove" event is fired. It removes the record(s) from the recordset.
*
* @method _defRemoveHash
* @private
*/
_defRemoveHash: function(e) {
i = 0;
}
},
/**
* @description Method called whenever "recordset:update" event is fired. It updates the record(s) by adding the new ones and removing the overwritten ones.
*
* @method _defUpdateHash
* @private
*/
_defUpdateHash: function(e) {
i = 0;
//deletes the object key that held on to an overwritten record and
//creates an object key to hold on to the updated record
if (e.overwritten[i]) {
}
}
},
/**
* @description Method called whenever "recordset:empty" event is fired. It empties the hash table.
*
* @method _defEmptyHash
* @private
*/
_defEmptyHash: function() {
this.set('table', {});
},
/**
* @description Sets up the hashtable with all the records currently in the recordset
*
* @method _setHashTable
* @private
*/
_setHashTable: function() {
var obj = {},
i = 0;
//If it is not an empty recordset - go through and set up the hash table.
for (; i < len; i++) {
}
}
return obj;
},
/**
* @description Helper method - it takes an object bag and converts it to a Y.Record
*
* @method _changeToRecord
* @param obj {Object || Y.Record} Any objet literal or Y.Record instance
* @return {Y.Record} A Record instance.
* @private
*/
_changeToRecord: function(obj) {
var oRec;
}
else {
});
}
return oRec;
},
//---------------------------------------------
// Events
//---------------------------------------------
/**
* @description Event that is fired whenever the recordset is changed. Note that multiple simultaneous changes still fires this event once. (ie: Adding multiple records via an array will only fire this event once at the completion of all the additions)
*
* @method _recordSetUpdated
* @private
*/
_recordsetChanged: function() {
function() {
Y.log('change fire');
this.fire('change', {});
});
},
/**
* @description Syncs up the private hash methods with their appropriate triggering events.
*
* @method _syncHashTable
* @private
*/
_syncHashTable: function() {
this.after('add',
function(e) {
this._defAddHash(e);
});
this.after('remove',
function(e) {
this._defRemoveHash(e);
});
this.after('update',
function(e) {
this._defUpdateHash(e);
});
this.after('empty',
function(e) {
this._defEmptyHash();
});
},
//---------------------------------------------
// Public Methods
//---------------------------------------------
/**
* @description Returns the record with particular ID or index
*
* @method getRecord
* @param i {String, Number} The ID of the record if a string, or the index if a number.
* @return {Y.Record} An Y.Record instance
* @public
*/
getRecord: function(i) {
return this.get('table')[i];
}
return this._items[i];
}
return null;
},
/**
* @description Returns the record at a particular index
*
* @method getRecordByIndex
* @param i {Number} Index at which the required record resides
* @return {Y.Record} An Y.Record instance
* @public
*/
getRecordByIndex: function(i) {
return this._items[i];
},
/**
* @description Returns a range of records beginning at particular index
*
* @method getRecordsByIndex
* @param index {Number} Index at which the required record resides
* @param range {Number} (Optional) Number of records to retrieve. The default is 1
* @return {Array} An array of Y.Record instances
* @public
*/
var i = 0,
returnedRecords = [];
//Range cannot take on negative values
for (; i < range; i++) {
}
return returnedRecords;
},
/**
* @description Returns the length of the recordset
*
* @method getLength
* @return {Number} Number of records in the recordset
* @public
*/
getLength: function() {
return this.size();
},
/**
* @description Returns an array of values for a specified key in the recordset
*
* @method getValuesByKey
* @param index {Number} (optional) Index at which the required record resides
* @return {Array} An array of values for the given key
* @public
*/
getValuesByKey: function(key) {
var i = 0,
retVals = [];
for (; i < len; i++) {
}
return retVals;
},
/**
* @description 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.
*
* @method add
* @param oData {Y.Record, Object Literal, Array} A Y.Record instance, An object literal of data or an array of object literals
* @param index {Number} (optional) Index at which to add the record(s)
* @return {Y.Recordset} The updated recordset instance
* @public
*/
var newRecords = [],
idx,
i = 0;
//Passing in array of object literals for oData
}
}
}
this.fire('add', {
});
return this;
},
/**
* @description Removes one or more Records to the RecordSet at the given index. If index is null, then removes a single Record from the end of the RecordSet.
*
* @method remove
* @param index {Number} (optional) Index at which to remove the record(s) from
* @param range {Number} (optional) Number of records to remove (including the one at the index)
* @return {Y.Recordset} The updated recordset instance
* @public
*/
var remRecords = [];
//Default is to only remove the last record - the length is always 1 greater than the last index
this.fire('remove', {
});
//this._recordRemoved(remRecords, index);
//return ({data: remRecords, index:index});
return this;
},
/**
* @description Empties the recordset
*
* @method empty
* @return {Y.Recordset} The updated recordset instance
* @public
*/
empty: function() {
this.fire('empty', {});
return this;
},
/**
* @description Updates the recordset with the new records passed in. Overwrites existing records when updating the index with the new records.
*
* @method update
* @param data {Y.Record, Object Literal, Array} A Y.Record instance, An object literal of data or an array of object literals
* @param index {Number} (optional) The index to start updating from.
* @return {Y.Recordset} The updated recordset instance
* @public
*/
var rec,
arr,
i = 0;
//Whatever is passed in, we are changing it to an array so that it can be easily iterated in the _defUpdateFn method
}
this.fire('update', {
});
return this;
}
},
{
ATTRS: {
/**
* @description An array of records that the recordset is storing
*
* @attribute records
* @public
* @type array
*/
records: {
getter: function() {
// give them a copy, not the internal object
return new Y.Array(this._items);
},
//For allData passed in here, see if each instance is a Record.
//If its not, change it to a record.
//Then push it into the array.
var records = [];
function initRecord(oneData) {
var o;
}
else {
o = new Y.Record({
});
}
}
//This conditional statement handles creating empty recordsets
if (allData) {
}
},
//value: [],
//initialization of the attribute must be done before the first call to get('records') is made.
//if lazyAdd were set to true, then instantiating using new Y.Recordset({records:[..]}) would
//not call the setter.
//see http://developer.yahoo.com/yui/3/api/Attribute.html#method_addAttr for details on this
lazyAdd: false
},
/**
* @description A hash table where the ID of the record is the key, and the record instance is the value.
*
* @attribute table
* @public
* @type object
*/
table: {
//Initially, create the hash table with all records currently in the recordset
valueFn: '_setHashTable'
},
/**
* @description The ID to use as the key in the hash table.
*
* @attribute key
* @public
* @type string
*/
key: {
value: 'id',
//set to readonly true. If you want custom hash tables, you should use the recordset-indexer plugin.
readOnly: true
}
}
});