cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly<div class="intro">
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly <p>The Recordset Utility provides a standard way for dealing with a collection of similar objects known as records. The `recordset-base` sub-module provides the base Recordset implementation.<p>
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith <p>This example demonstrates some standard ways to interact with the Recordset. Click on the buttons below to play around with a Recordset created with state information.</p>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly<div class="example yui3-skin-sam">
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith{{>recordset-basic-source}}
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith<p>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.</p>
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith<p>Recordset-base provides an API for adding, removing and updating record(s). It also creates a hash table of all the records by their IDs, to allow for fast retrieval.</p>
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith<h6>Records vs. Object Literals</h6>
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith<p>You can interact with a Recordset instance by simply passing in object literals. Recordset will convert them into record instances under the hood. If you want the object literal back from the Recordset, simply use the `getValue` method on a record instance. Optionally, a string can be passed into the `getValue` method if you only want a specific property back. Not passing in an argument returns the entire object bag that the record is holding on to.
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith<h3>Working with a Recordset</h3>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny DonnellyThis section describes how to use the Recordset Utility in further detail. It contains the following subsections:
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly<ul class="topspace">
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith <li><a href="#instantiation">Initializing a Recordset</a></li>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly <li><a href="#adding">Adding Records</a></li>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly <li><a href="#removing">Removing Records</a></li>
819e90d415ed17d59af3a247b2ad9d6feb0c21b5Luke Smith <li><a href="#updating">Updating Records</a></li>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly <li><a href="#emptying">Emptying a Recordset</a></li>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly <li><a href="#hash">Using the Hash Table</a></li>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly<h4 id="instantiation">Initializing a Recordset</h4>
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny DonnellyYUI().use("recordset-base", function(Y) {
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly {a:3, b:2, c:1},
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly {a:9, b:8, c:7},
cf6c1ae1ed15095f8dc269bb9d7a373a1b87990eJenny Donnelly {a:1, b:2, c:3}
myRecordset = new Y.Recordset({records: data}),
anEmptyRecordset = new Y.Recordset();
<p>Once initialized, the Recordset can be modified by CRUD methods provided by the API. Add single or multiple objects/records to the Recordset by calling the `add` method.</p>
myRecordset = new Y.Recordset({records:data});
myRecordset.add({key:"d", label:"Column D"});
myRecordset = new Y.Recordset({records:data});
myRecordset.remove(0,2);
<p>You can overwrite existing records in the Recordset through the `update` method. This method takes 2 arguments, the first being an object/record or array of objects/records, and the second being the index at which to start the update process. When updating a Recordset, any records found within the index/indices that are to be updated will be overwritten. However, you can get an object bag with these overwritten records back if you hook onto the 'update' event. </p>
myRecordset = new Y.Recordset({records:data});
myRecordset.update({key:"d", label:"Column D"}, 2);
myRecordset = new Y.Recordset({records:data});
<p>The recordset-base submodule has a built-in hash table that hashes records by their IDs. IDs are set during initialization of each individual record and are stored as ATTRS. You should not modify the ID of a record, but you can access it by calling `myRecord.get('id')`.</p>
<p>Under the hood, the hash table is kept in sync with the Recordset by hooking onto the events that Recordset fires (namely 'add', 'remove', 'update', and 'empty'). The ID hash table is an easy way to retrieve records that will be needed often.</p>
<p>If you need hash tables that store records by keys other than IDs, refer to the <strong>RecordsetIndexer</strong> plugin.</p>
myRecordset = new Y.Recordset({records:data});
var record2_ID = myRecordset.getRecord(1).get('id'),
record3_ID = myRecordset.getRecord(2).get('id');
var hashTable = myRecordset.get('table');
<p>The Recordset Utility fires 5 custom events in addition attribute change events: <strong>'add'</strong>, <strong>'remove'</strong>, <strong>'update'</strong>, <strong>'empty'</strong> and <strong>'change'</strong>. <strong>'change'</strong> in particular, is a high-level event that is fired after any modifications to the Recordset are made (ie: after 'add', 'remove', 'update', 'empty'). Details on these events are shown below.</p>
<td><strong>added:</strong> an array of new records that were added (can contain a single record)<br/>
<td><strong>removed:</strong> an array of records that were removed (can contain a single record)<br/>
<td><strong>type:</strong> The event that caused the change (ie: "recordset:add", "recordset:update")<br/>