5
<title>Recordset</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro component">
24
The Recordset utility allows the storage and retrieval of objects with
29
<h2 id="getting-started">Getting Started</h2>
32
To include the source files for Recordset and its dependencies, first load
33
the YUI seed file if you haven't already loaded it.
36
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script></pre>
40
Next, create a new YUI instance for your application and populate it with the
41
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
42
YUI will automatically load any dependencies required by the modules you
46
<pre class="code prettyprint">// Create a new YUI instance and populate it with the required modules.
47
YUI().use('recordset', function (Y) {
48
// Recordset is available and ready for use. Add implementation
49
// code here.
54
For more information on creating YUI instances and on the
55
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
56
documentation for the <a href="../yui/index.html">YUI Global object</a>.
60
<h2 id="using">Using Recordsets</h2>
62
<h3 id="basics">Recordset basics</h3>
65
A Recordset in its simplest form is a collection of records, where records
66
can be considered to be object literals. Recordset allows the user to
67
handle this collection of records with a consistent API.
71
Recordset augments the functionality of <code>Y.Arraylist</code> but goes a
72
step further, by allowing the developer to quickly store and retrieve
73
objects with similar properties. Additional submodules can be plugged into
74
a <code>Y.Recordset</code> instance to enable sorting, filtering and
75
indexing by specific keys.
79
Initializing a Recordset is straight-forward:
82
<pre class="code prettyprint">YUI().use("recordset-base", function(Y) {
89
//Recordset is created with the objects from the data array
90
myRecordset = new Y.Recordset({records: data}),
92
//Empty Recordsets can also be created
93
anEmptyRecordset = new Y.Recordset();
99
<h3 id="crud">Adding, Removing, Updating and Emptying</h3>
102
A <code>Y.Recordset</code> can be filled with a single, or an array of
103
object literals. Under the hood, Recordset will convert these objects into
104
<code>record</code> instances - essentially creating a light wrapper around
109
More information on performing operations on Recordset can be seen the the
110
documentation for the <a href="recordset-basic.html">
111
<code>recordset-base</code> sub-module</a>.
114
<h4 id="add">Adding Records</h4>
116
<pre class="code prettyprint">var data = [
117
{key:"a", label:"Column A"},
118
{key:"b", label:"Column B"},
119
{key:"c", label:"Column C"}
121
myRecordset = new Y.Recordset({records:data});
123
//Adding a single record to the end of a Recordset
124
myRecordset.add({key:"d", label:"Column D"});
126
//Adding multiple records at the 2nd index of the Recordset
128
{key:"e", label:"Column E"},
129
{key:"f", label:"Column F"}
133
<h4 id="remove">Removing Records</h4>
135
<pre class="code prettyprint">var data = [
136
{key:"a", label:"Column A"},
137
{key:"b", label:"Column B"},
138
{key:"c", label:"Column C"}
140
myRecordset = new Y.Recordset({records:data});
142
//removes the record stored at index 2 (in this case {key:"c", label:"Column C"} is removed)
143
myRecordset.remove(2);
145
//Removes 2 records starting at index zero
146
myRecordset.remove(0,2);</pre>
149
<h4 id="updating-records">Updating Records</h4>
151
<pre class="code prettyprint">var data = [
152
{key:"a", label:"Column A"},
153
{key:"b", label:"Column B"},
154
{key:"c", label:"Column C"}
157
myRecordset = new Y.Recordset({records:data});
159
//overwite the record at index 2 with the following record
160
myRecordset.update({key:"d", label:"Column D"}, 2);
162
//You can also update multiple records at a time.
163
//Here we are updating indices 0 and 1 of the Recordset with the corresponding two objects.
165
{key:"e", label:"Column E"},
166
{key: "f", label: "Column F"}
170
<h4 id="emptying-a-recordset">Emptying a Recordset</h4>
172
<pre class="code prettyprint">var data = [
173
{key:"a", label:"Column A"},
174
{key:"b", label:"Column B"},
175
{key:"c", label:"Column C"}
178
myRecordset = new Y.Recordset({records:data});
179
myRecordset.empty();</pre>
182
<h3 id="events">Events</h3>
185
The Recordset Utility fires custom events in addition attribute change
186
events. Details on these events are shown below. The sub-module responsible
187
for firing each event is represented in square braces.
193
<th>Event [sub-module]</th>
199
<td><code>add</code> [base]</td>
200
<td><strong>added:</strong> an array of new records that were added (can contain a single record)<br/>
201
<strong>index:</strong> index that the addition started at
205
<td><code>remove</code> [base]</td>
206
<td><strong>removed:</strong> an array of records that were removed (can contain a single record)<br/>
207
<strong>index:</strong> index that the removals started at<br/>
208
<strong>range:</strong> range of records that were removed
212
<td><code>update</code> [base]</td>
213
<td><strong>updated:</strong> an array of records that updated (added to the Recordset)<br/>
214
<strong>index:</strong> index that the updates started at<br/>
215
<strong>range:</strong> range of records that were updated
219
<td><code>empty</code> [base]</td>
220
<td>Empty object bag</td>
223
<td><code>change</code> [base]</td>
224
<td>Empty object bag, fired whenever records in the Recordset change (ie: they are added, removed, updated, or emptied)</td>
227
<td><code>sort</code> [sort]</td>
228
<td><strong>field:</strong> A string representing the key to sort by<br/>
229
<strong>desc:</strong> Boolean representing whether sorting order is descending<br/>
230
<strong>sorter:</strong> The comparison function being used to sort
236
<h3 id="sorting">Sorting</h3>
239
The <code>RecordsetSort</code> plugin allows a Recordset to have default
240
and custom sorting functionality. Various helper methods and attributes are
241
provided. A brief listing is shown below. Refer to the documentation for
242
the <a href="recordset-sort.html">
243
<code>RecordsetSort</code> plugin</a> to see code snippets.
247
<dt><code>sort(key,desc,function [optional])</code></dt>
249
Sorts a Recordset by the value in the provided key. Recordset is
250
sorted in descending order if <code>desc</code> is truthy. Optional
251
custom comparison function can be supplied to sort by.
253
<dt><code>resort()</code></dt>
254
<dd>Resorts a Recordset using last-used sorting arguments</dd>
255
<dt><code>reverse()</code></dt>
256
<dd>Reverses the Recordset without performing any sort operations</dd>
257
<dt><code>flip()</code></dt>
259
Resorts the Recordset in ascending order if the last sort arguments
260
specified descending, and vice versa.
262
<dt><code>lastSortProperties Attribute</code></dt>
264
An object bag with <code>field</code>, <code>desc</code> and
265
<code>sorter</code> keys listing the last-used arguments to sort
268
<dt><code>isSorted Attribute</code></dt>
269
<dd>A boolean representing if the Recordset is in a sorted state.</dd>
272
<h3 id="filtering">Filtering</h3>
275
The <code>RecordsetFilter</code> plugin allows a Recordset to be filtered,
276
and returns subsets of that Recordset (as a separate Recordset instance).
277
Refer to the documentation for the <a
278
href="recordset-filter.html">
279
<code>RecordsetFilter</code> plugin to see code snippets.</a>
283
<dt><code>filter(key or function, value)</code></dt>
285
If a key/value pair is passed in, returns a Recordset with records
286
that match the key/value pair. Supports a custom function for more
287
advanced filtering. The custom function must return a boolean.
289
<dt><code>reject(function)</code></dt>
291
The inverse of filter. Executes the supplied function on each item.
292
Returns a new Recordset containing the items that the supplied
293
function returned <code>false</code> for.
295
<dt><code>grep(pattern)</code></dt>
297
Iterates over the Recordset, returning a new Recordset of all the
298
elements that match the supplied regular expression
303
<h3 id="indexing">Indexing</h3>
306
In the <code>recordset-base</code> submodule, the <code>table</code>
307
attribute stores an associative array that hashes all records within the
308
Recordset by their unique YUIDs. This table is kept in sync with the
309
Recordset through custom events.
313
To allow for customization when indexing, there is the
314
<code>RecordsetIndexer</code> plugin. Refer to the <a
315
href="recordset-indexer.html"><code>RecordsetIndexer</code>
316
documentation</a> for full details.
321
<dt><code>createTable(key)</code></dt>
323
Creates a hash table with the given key with all existing records
326
<dt><code>getTable(key)</code></dt>
327
<dd>Get a hash table that hashes records by a given key.</dd>
328
<dt><code>hashTables</code> attribute</dt>
330
An object bag, with each property referring to a user-created hash
337
<div id="sidebar" class="yui3-u">
339
<div id="toc" class="sidebox">
341
<h2 class="no-toc">Table of Contents</h2>
347
<a href="#getting-started">Getting Started</a>
350
<a href="#using">Using Recordsets</a>
353
<a href="#basics">Recordset basics</a>
356
<a href="#crud">Adding, Removing, Updating and Emptying</a>
359
<a href="#add">Adding Records</a>
362
<a href="#remove">Removing Records</a>
365
<a href="#updating-records">Updating Records</a>
368
<a href="#emptying-a-recordset">Emptying a Recordset</a>
373
<a href="#events">Events</a>
376
<a href="#sorting">Sorting</a>
379
<a href="#filtering">Filtering</a>
382
<a href="#indexing">Indexing</a>
392
<div class="sidebox">
394
<h2 class="no-toc">Examples</h2>
398
<ul class="examples">
401
<li data-description="Shows how to instantiate and interact with a standard Recordset by adding, removing and updating records/objects. Explains the events that are fired, and shows how to access the built-in hash table that stores records by their YUIDs.">
402
<a href="recordset-basic.html">Basic Recordset</a>
407
<li data-description="Shows how to sort a Recordset using the default sorter, or a custom sort function. Additionally, demonstrates how convenience attributes help in determining when Recordset is in a sorted state, and what the last sort parameters were.">
408
<a href="recordset-sort.html">Recordset Sort Plugin</a>
413
<li data-description="Shows how to use filtering methods to Recordset. Key-value filtering, and filtering by providing a custom function are covered.">
414
<a href="recordset-filter.html">Recordset Filter Plugin</a>
419
<li data-description="Shows how to create and access hash tables on Recordset. Tables are kept in sync through events fired by Recordset.">
420
<a href="recordset-indexer.html">Recordset Indexer Plugin</a>
434
<script src="../assets/vendor/prettify/prettify-min.js"></script>
435
<script>prettyPrint();</script>