2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('recordset-indexer', function (Y, NAME) {
11 * Provides the ability to store multiple custom hash tables referencing records in the recordset.
13 * @submodule recordset-indexer
16 * Plugin that provides the ability to store multiple custom hash tables referencing records in the recordset.
17 * This utility does not support any collision handling. New hash table entries with a used key overwrite older ones.
18 * @class RecordsetIndexer
20 function RecordsetIndexer(config) {
21 RecordsetIndexer.superclass.constructor.apply(this, arguments);
24 Y.mix(RecordsetIndexer, {
27 NAME: "recordsetIndexer",
31 * @description Collection of all the hashTables created by the plugin.
32 * The individual tables can be accessed by the key they are hashing against.
34 * @attribute hashTables
54 Y.extend(RecordsetIndexer, Y.Plugin.Base, {
56 initializer: function(config) {
57 var host = this.get('host');
59 //setup listeners on recordset events
60 this.onHostEvent('add', Y.bind("_defAddHash", this), host);
61 this.onHostEvent('remove', Y.bind('_defRemoveHash', this), host);
62 this.onHostEvent('update', Y.bind('_defUpdateHash', this), host);
66 destructor: function(config) {
72 * @description Setup the hash table for a given key with all existing records in the recordset
74 * @method _setHashTable
75 * @param key {string} A key to hash by.
76 * @return obj {object} The created hash table
79 _setHashTable: function(key) {
80 var host = this.get('host'),
83 len = host.getLength();
85 for (; i < len; i++) {
86 obj[host._items[i].getValue(key)] = host._items[i];
91 //---------------------------------------------
93 //---------------------------------------------
96 * @description Updates all hash tables when a record is added to the recordset
101 _defAddHash: function(e) {
102 var tbl = this.get('hashTables');
105 //Go through every hashtable that is stored.
106 //in each hashtable, look to see if the key is represented in the object being added.
109 Y.each(e.added || e.updated,
111 //if the object being added has a key which is being stored by hashtable v, add it into the table.
112 if (o.getValue(key)) {
113 v[o.getValue(key)] = o;
121 * @description Updates all hash tables when a record is removed from the recordset
123 * @method _defRemoveHash
126 _defRemoveHash: function(e) {
127 var tbl = this.get('hashTables'),
130 //Go through every hashtable that is stored.
131 //in each hashtable, look to see if the key is represented in the object being deleted.
134 Y.each(e.removed || e.overwritten,
136 reckey = o.getValue(key);
138 //if the hashtable has a key storing a record, and the key and the record both match the record being deleted, delete that row from the hashtable
139 if (reckey && v[reckey] === o) {
147 * @description Updates all hash tables when the recordset is updated (a combination of add and remove)
149 * @method _defUpdateHash
152 _defUpdateHash: function(e) {
154 //TODO: It will be more performant to create a new method rather than using _defAddHash, _defRemoveHash, due to the number of loops. See commented code.
156 e.removed = e.overwritten;
158 this._defRemoveHash(e);
161 var tbl = this.get('hashTables'), reckey;
163 Y.each(tbl, function(v, key) {
164 Y.each(e.updated, function(o, i) {
166 //delete record from hashtable if it has been overwritten
167 reckey = o.getValue(key);
173 //the undefined case is if more records are updated than currently exist in the recordset.
174 if (e.overwritten[i] && (v[e.overwritten[i].getValue(key)] === e.overwritten[i])) {
175 delete v[e.overwritten[i].getValue(key)];
178 // if (v[reckey] === o) {
182 // //add the new updated record if it has a key that corresponds to a hash table
183 // if (o.getValue(key)) {
184 // v[o.getValue(key)] = o;
192 //---------------------------------------------
194 //---------------------------------------------
197 * @description Creates a new hash table.
199 * @method createTable
200 * @param key {string} A key to hash by.
201 * @return tbls[key] {object} The created hash table
204 createTable: function(key) {
205 var tbls = this.get('hashTables');
206 tbls[key] = this._setHashTable(key);
207 this.set('hashTables', tbls);
214 * @description Get a hash table that hashes records by a given key.
217 * @param key {string} A key to hash by.
218 * @return table {object} The created hash table
221 getTable: function(key) {
222 return this.get('hashTables')[key];
230 Y.namespace("Plugin").RecordsetIndexer = RecordsetIndexer;
234 }, '3.13.0', {"requires": ["recordset-base", "plugin"]});