The Recordset utility allows the storage and retrieval of objects with similar properties.
A Recordset in its simplest form is a collection of records, where records can be considered to be object literals. Recordset allows the user to handle this collection of records with a consistent API.
Recordset augments the functionality of Y.Arraylist
but goes a
step further, by allowing the developer to quickly store and retrieve
objects with similar properties. Additional submodules can be plugged into
a Y.Recordset
instance to enable sorting, filtering and
indexing by specific keys.
Initializing a Recordset is straight-forward:
``` YUI().use("recordset-base", function(Y) { var data = [ {a:3, b:2, c:1}, {a:9, b:8, c:7}, {a:1, b:2, c:3} ], //Recordset is created with the objects from the data array myRecordset = new Y.Recordset({records: data}), //Empty Recordsets can also be created anEmptyRecordset = new Y.Recordset(); }); ```
A Y.Recordset
can be filled with a single, or an array of
object literals. Under the hood, Recordset will convert these objects into
record
instances - essentially creating a light wrapper around
them.
More information on performing operations on Recordset can be seen the the
documentation for the
recordset-base
sub-module.
The Recordset Utility fires custom events in addition attribute change events. Details on these events are shown below. The sub-module responsible for firing each event is represented in square braces.
Event [sub-module] | Payload |
---|---|
add [base] |
added: an array of new records that were added (can contain a single record) index: index that the addition started at |
remove [base] |
removed: an array of records that were removed (can contain a single record) index: index that the removals started at range: range of records that were removed |
update [base] |
updated: an array of records that updated (added to the Recordset) index: index that the updates started at range: range of records that were updated |
empty [base] |
Empty object bag |
change [base] |
Empty object bag, fired whenever records in the Recordset change (ie: they are added, removed, updated, or emptied) |
sort [sort] |
field: A string representing the key to sort by desc: Boolean representing whether sorting order is descending sorter: The comparison function being used to sort |
The RecordsetSort
plugin allows a Recordset to have default
and custom sorting functionality. Various helper methods and attributes are
provided. A brief listing is shown below. Refer to the documentation for
the
RecordsetSort
plugin to see code snippets.
sort(key,desc,function [optional])
desc
is truthy. Optional
custom comparison function can be supplied to sort by.
resort()
reverse()
flip()
lastSortProperties Attribute
field
, desc
and
sorter
keys listing the last-used arguments to sort
by.
isSorted Attribute
The RecordsetFilter
plugin allows a Recordset to be filtered,
and returns subsets of that Recordset (as a separate Recordset instance).
Refer to the documentation for the
RecordsetFilter
plugin to see code snippets.
filter(key or function, value)
reject(function)
false
for.
grep(pattern)
In the recordset-base
submodule, the table
attribute stores an associative array that hashes all records within the
Recordset by their unique YUIDs. This table is kept in sync with the
Recordset through custom events.
To allow for customization when indexing, there is the
RecordsetIndexer
plugin. Refer to the RecordsetIndexer
documentation for full details.
createTable(key)
getTable(key)
hashTables
attribute