5
<title>Recordset</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro component">
26
The Recordset utility allows the storage and retrieval of objects with
31
<h2 id="getting-started">Getting Started</h2>
34
To include the source files for Recordset and its dependencies, first load
35
the YUI seed file if you haven't already loaded it.
38
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.5.0/build/yui/yui-min.js"></script></pre>
42
Next, create a new YUI instance for your application and populate it with the
43
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
44
YUI will automatically load any dependencies required by the modules you
48
<pre class="code prettyprint"><script>
49
// Create a new YUI instance and populate it with the required modules.
50
YUI().use('recordset', function (Y) {
51
// Recordset is available and ready for use. Add implementation
52
// code here.
54
</script></pre>
58
For more information on creating YUI instances and on the
59
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
60
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
64
<h2 id="using">Using Recordsets</h2>
66
<h3 id="basics">Recordset basics</h3>
69
A Recordset in its simplest form is a collection of records, where records
70
can be considered to be object literals. Recordset allows the user to
71
handle this collection of records with a consistent API.
75
Recordset augments the functionality of <code>Y.Arraylist</code> but goes a
76
step further, by allowing the developer to quickly store and retrieve
77
objects with similar properties. Additional submodules can be plugged into
78
a <code>Y.Recordset</code> instance to enable sorting, filtering and
79
indexing by specific keys.
83
Initializing a Recordset is straight-forward:
86
<pre class="code prettyprint">YUI().use("recordset-base", function(Y) {
93
//Recordset is created with the objects from the data array
94
myRecordset = new Y.Recordset({records: data}),
96
//Empty Recordsets can also be created
97
anEmptyRecordset = new Y.Recordset();
103
<h3 id="crud">Adding, Removing, Updating and Emptying</h3>
106
A <code>Y.Recordset</code> can be filled with a single, or an array of
107
object literals. Under the hood, Recordset will convert these objects into
108
<code>record</code> instances - essentially creating a light wrapper around
113
More information on performing operations on Recordset can be seen the the
114
documentation for the <a href="recordset-basic.html">
115
<code>recordset-base</code> sub-module</a>.
118
<h4 id="add">Adding Records</h4>
120
<pre class="code prettyprint">var data = [
121
{key:"a", label:"Column A"},
122
{key:"b", label:"Column B"},
123
{key:"c", label:"Column C"}
125
myRecordset = new Y.Recordset({records:data});
127
//Adding a single record to the end of a Recordset
128
myRecordset.add({key:"d", label:"Column D"});
130
//Adding multiple records at the 2nd index of the Recordset
132
{key:"e", label:"Column E"},
133
{key:"f", label:"Column F"}
137
<h4 id="remove">Removing Records</h4>
139
<pre class="code prettyprint">var data = [
140
{key:"a", label:"Column A"},
141
{key:"b", label:"Column B"},
142
{key:"c", label:"Column C"}
144
myRecordset = new Y.Recordset({records:data});
146
//removes the record stored at index 2 (in this case {key:"c", label:"Column C"} is removed)
147
myRecordset.remove(2);
149
//Removes 2 records starting at index zero
150
myRecordset.remove(0,2);</pre>
153
<h4 id="updating-records">Updating Records</h4>
155
<pre class="code prettyprint">var data = [
156
{key:"a", label:"Column A"},
157
{key:"b", label:"Column B"},
158
{key:"c", label:"Column C"}
161
myRecordset = new Y.Recordset({records:data});
163
//overwite the record at index 2 with the following record
164
myRecordset.update({key:"d", label:"Column D"}, 2);
166
//You can also update multiple records at a time.
167
//Here we are updating indices 0 and 1 of the Recordset with the corresponding two objects.
169
{key:"e", label:"Column E"},
170
{key: "f", label: "Column F"}
174
<h4 id="emptying-a-recordset">Emptying a Recordset</h4>
176
<pre class="code prettyprint">var data = [
177
{key:"a", label:"Column A"},
178
{key:"b", label:"Column B"},
179
{key:"c", label:"Column C"}
182
myRecordset = new Y.Recordset({records:data});
183
myRecordset.empty();</pre>
186
<h3 id="events">Events</h3>
189
The Recordset Utility fires custom events in addition attribute change
190
events. Details on these events are shown below. The sub-module responsible
191
for firing each event is represented in square braces.
197
<th>Event [sub-module]</th>
203
<td><code>add</code> [base]</td>
204
<td><strong>added:</strong> an array of new records that were added (can contain a single record)<br/>
205
<strong>index:</strong> index that the addition started at
209
<td><code>remove</code> [base]</td>
210
<td><strong>removed:</strong> an array of records that were removed (can contain a single record)<br/>
211
<strong>index:</strong> index that the removals started at<br/>
212
<strong>range:</strong> range of records that were removed
216
<td><code>update</code> [base]</td>
217
<td><strong>updated:</strong> an array of records that updated (added to the Recordset)<br/>
218
<strong>index:</strong> index that the updates started at<br/>
219
<strong>range:</strong> range of records that were updated
223
<td><code>empty</code> [base]</td>
224
<td>Empty object bag</td>
227
<td><code>change</code> [base]</td>
228
<td>Empty object bag, fired whenever records in the Recordset change (ie: they are added, removed, updated, or emptied)</td>
231
<td><code>sort</code> [sort]</td>
232
<td><strong>field:</strong> A string representing the key to sort by<br/>
233
<strong>desc:</strong> Boolean representing whether sorting order is descending<br/>
234
<strong>sorter:</strong> The comparison function being used to sort
240
<h3 id="sorting">Sorting</h3>
243
The <code>RecordsetSort</code> plugin allows a Recordset to have default
244
and custom sorting functionality. Various helper methods and attributes are
245
provided. A brief listing is shown below. Refer to the documentation for
246
the <a href="recordset-sort.html">
247
<code>RecordsetSort</code> plugin</a> to see code snippets.
251
<dt><code>sort(key,desc,function [optional])</code></dt>
253
Sorts a Recordset by the value in the provided key. Recordset is
254
sorted in descending order if <code>desc</code> is truthy. Optional
255
custom comparison function can be supplied to sort by.
257
<dt><code>resort()</code></dt>
258
<dd>Resorts a Recordset using last-used sorting arguments</dd>
259
<dt><code>reverse()</code></dt>
260
<dd>Reverses the Recordset without performing any sort operations</dd>
261
<dt><code>flip()</code></dt>
263
Resorts the Recordset in ascending order if the last sort arguments
264
specified descending, and vice versa.
266
<dt><code>lastSortProperties Attribute</code></dt>
268
An object bag with <code>field</code>, <code>desc</code> and
269
<code>sorter</code> keys listing the last-used arguments to sort
272
<dt><code>isSorted Attribute</code></dt>
273
<dd>A boolean representing if the Recordset is in a sorted state.</dd>
276
<h3 id="filtering">Filtering</h3>
279
The <code>RecordsetFilter</code> plugin allows a Recordset to be filtered,
280
and returns subsets of that Recordset (as a separate Recordset instance).
281
Refer to the documentation for the <a
282
href="recordset-filter.html">
283
<code>RecordsetFilter</code> plugin to see code snippets.</a>
287
<dt><code>filter(key or function, value)</code></dt>
289
If a key/value pair is passed in, returns a Recordset with records
290
that match the key/value pair. Supports a custom function for more
291
advanced filtering. The custom function must return a boolean.
293
<dt><code>reject(function)</code></dt>
295
The inverse of filter. Executes the supplied function on each item.
296
Returns a new Recordset containing the items that the supplied
297
function returned <code>false</code> for.
299
<dt><code>grep(pattern)</code></dt>
301
Iterates over the Recordset, returning a new Recordset of all the
302
elements that match the supplied regular expression
307
<h3 id="indexing">Indexing</h3>
310
In the <code>recordset-base</code> submodule, the <code>table</code>
311
attribute stores an associative array that hashes all records within the
312
Recordset by their unique YUIDs. This table is kept in sync with the
313
Recordset through custom events.
317
To allow for customization when indexing, there is the
318
<code>RecordsetIndexer</code> plugin. Refer to the <a
319
href="recordset-indexer.html"><code>RecordsetIndexer</code>
320
documentation</a> for full details.
325
<dt><code>createTable(key)</code></dt>
327
Creates a hash table with the given key with all existing records
330
<dt><code>getTable(key)</code></dt>
331
<dd>Get a hash table that hashes records by a given key.</dd>
332
<dt><code>hashTables</code> attribute</dt>
334
An object bag, with each property referring to a user-created hash
342
<div class="yui3-u-1-4">
343
<div class="sidebar">
345
<div id="toc" class="sidebox">
347
<h2 class="no-toc">Table of Contents</h2>
353
<a href="#getting-started">Getting Started</a>
356
<a href="#using">Using Recordsets</a>
359
<a href="#basics">Recordset basics</a>
362
<a href="#crud">Adding, Removing, Updating and Emptying</a>
365
<a href="#add">Adding Records</a>
368
<a href="#remove">Removing Records</a>
371
<a href="#updating-records">Updating Records</a>
374
<a href="#emptying-a-recordset">Emptying a Recordset</a>
379
<a href="#events">Events</a>
382
<a href="#sorting">Sorting</a>
385
<a href="#filtering">Filtering</a>
388
<a href="#indexing">Indexing</a>
398
<div class="sidebox">
400
<h2 class="no-toc">Examples</h2>
404
<ul class="examples">
407
<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.">
408
<a href="recordset-basic.html">Basic Recordset</a>
413
<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.">
414
<a href="recordset-sort.html">Recordset Sort Plugin</a>
419
<li data-description="Shows how to use filtering methods to Recordset. Key-value filtering, and filtering by providing a custom function are covered.">
420
<a href="recordset-filter.html">Recordset Filter Plugin</a>
425
<li data-description="Shows how to create and access hash tables on Recordset. Tables are kept in sync through events fired by Recordset.">
426
<a href="recordset-indexer.html">Recordset Indexer Plugin</a>
441
<script src="../assets/vendor/prettify/prettify-min.js"></script>
442
<script>prettyPrint();</script>