| 1 |
efrain |
1 |
YUI.add('recordset-indexer', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Provides the ability to store multiple custom hash tables referencing records in the recordset.
|
|
|
5 |
* @module recordset
|
|
|
6 |
* @submodule recordset-indexer
|
|
|
7 |
*/
|
|
|
8 |
/**
|
|
|
9 |
* Plugin that provides the ability to store multiple custom hash tables referencing records in the recordset.
|
|
|
10 |
* This utility does not support any collision handling. New hash table entries with a used key overwrite older ones.
|
|
|
11 |
* @class RecordsetIndexer
|
|
|
12 |
*/
|
|
|
13 |
function RecordsetIndexer(config) {
|
|
|
14 |
RecordsetIndexer.superclass.constructor.apply(this, arguments);
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
Y.mix(RecordsetIndexer, {
|
|
|
18 |
NS: "indexer",
|
|
|
19 |
|
|
|
20 |
NAME: "recordsetIndexer",
|
|
|
21 |
|
|
|
22 |
ATTRS: {
|
|
|
23 |
/**
|
|
|
24 |
* @description Collection of all the hashTables created by the plugin.
|
|
|
25 |
* The individual tables can be accessed by the key they are hashing against.
|
|
|
26 |
*
|
|
|
27 |
* @attribute hashTables
|
|
|
28 |
* @public
|
|
|
29 |
* @type object
|
|
|
30 |
*/
|
|
|
31 |
hashTables: {
|
|
|
32 |
value: {
|
|
|
33 |
|
|
|
34 |
}
|
|
|
35 |
},
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
keys: {
|
|
|
39 |
value: {
|
|
|
40 |
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
});
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
Y.extend(RecordsetIndexer, Y.Plugin.Base, {
|
|
|
48 |
|
|
|
49 |
initializer: function(config) {
|
|
|
50 |
var host = this.get('host');
|
|
|
51 |
|
|
|
52 |
//setup listeners on recordset events
|
|
|
53 |
this.onHostEvent('add', Y.bind("_defAddHash", this), host);
|
|
|
54 |
this.onHostEvent('remove', Y.bind('_defRemoveHash', this), host);
|
|
|
55 |
this.onHostEvent('update', Y.bind('_defUpdateHash', this), host);
|
|
|
56 |
|
|
|
57 |
},
|
|
|
58 |
|
|
|
59 |
destructor: function(config) {
|
|
|
60 |
|
|
|
61 |
},
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @description Setup the hash table for a given key with all existing records in the recordset
|
|
|
66 |
*
|
|
|
67 |
* @method _setHashTable
|
|
|
68 |
* @param key {string} A key to hash by.
|
|
|
69 |
* @return obj {object} The created hash table
|
|
|
70 |
* @private
|
|
|
71 |
*/
|
|
|
72 |
_setHashTable: function(key) {
|
|
|
73 |
var host = this.get('host'),
|
|
|
74 |
obj = {},
|
|
|
75 |
i = 0,
|
|
|
76 |
len = host.getLength();
|
|
|
77 |
|
|
|
78 |
for (; i < len; i++) {
|
|
|
79 |
obj[host._items[i].getValue(key)] = host._items[i];
|
|
|
80 |
}
|
|
|
81 |
return obj;
|
|
|
82 |
},
|
|
|
83 |
|
|
|
84 |
//---------------------------------------------
|
|
|
85 |
// Syncing Methods
|
|
|
86 |
//---------------------------------------------
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @description Updates all hash tables when a record is added to the recordset
|
|
|
90 |
*
|
|
|
91 |
* @method _defAddHash
|
|
|
92 |
* @private
|
|
|
93 |
*/
|
|
|
94 |
_defAddHash: function(e) {
|
|
|
95 |
var tbl = this.get('hashTables');
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
//Go through every hashtable that is stored.
|
|
|
99 |
//in each hashtable, look to see if the key is represented in the object being added.
|
|
|
100 |
Y.each(tbl,
|
|
|
101 |
function(v, key) {
|
|
|
102 |
Y.each(e.added || e.updated,
|
|
|
103 |
function(o) {
|
|
|
104 |
//if the object being added has a key which is being stored by hashtable v, add it into the table.
|
|
|
105 |
if (o.getValue(key)) {
|
|
|
106 |
v[o.getValue(key)] = o;
|
|
|
107 |
}
|
|
|
108 |
});
|
|
|
109 |
});
|
|
|
110 |
|
|
|
111 |
},
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* @description Updates all hash tables when a record is removed from the recordset
|
|
|
115 |
*
|
|
|
116 |
* @method _defRemoveHash
|
|
|
117 |
* @private
|
|
|
118 |
*/
|
|
|
119 |
_defRemoveHash: function(e) {
|
|
|
120 |
var tbl = this.get('hashTables'),
|
|
|
121 |
reckey;
|
|
|
122 |
|
|
|
123 |
//Go through every hashtable that is stored.
|
|
|
124 |
//in each hashtable, look to see if the key is represented in the object being deleted.
|
|
|
125 |
Y.each(tbl,
|
|
|
126 |
function(v, key) {
|
|
|
127 |
Y.each(e.removed || e.overwritten,
|
|
|
128 |
function(o) {
|
|
|
129 |
reckey = o.getValue(key);
|
|
|
130 |
|
|
|
131 |
//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
|
|
|
132 |
if (reckey && v[reckey] === o) {
|
|
|
133 |
delete v[reckey];
|
|
|
134 |
}
|
|
|
135 |
});
|
|
|
136 |
});
|
|
|
137 |
},
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* @description Updates all hash tables when the recordset is updated (a combination of add and remove)
|
|
|
141 |
*
|
|
|
142 |
* @method _defUpdateHash
|
|
|
143 |
* @private
|
|
|
144 |
*/
|
|
|
145 |
_defUpdateHash: function(e) {
|
|
|
146 |
|
|
|
147 |
//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.
|
|
|
148 |
e.added = e.updated;
|
|
|
149 |
e.removed = e.overwritten;
|
|
|
150 |
this._defAddHash(e);
|
|
|
151 |
this._defRemoveHash(e);
|
|
|
152 |
|
|
|
153 |
/*
|
|
|
154 |
var tbl = this.get('hashTables'), reckey;
|
|
|
155 |
|
|
|
156 |
Y.each(tbl, function(v, key) {
|
|
|
157 |
Y.each(e.updated, function(o, i) {
|
|
|
158 |
|
|
|
159 |
//delete record from hashtable if it has been overwritten
|
|
|
160 |
reckey = o.getValue(key);
|
|
|
161 |
|
|
|
162 |
if (reckey) {
|
|
|
163 |
v[reckey] = o;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
//the undefined case is if more records are updated than currently exist in the recordset.
|
|
|
167 |
if (e.overwritten[i] && (v[e.overwritten[i].getValue(key)] === e.overwritten[i])) {
|
|
|
168 |
delete v[e.overwritten[i].getValue(key)];
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
// if (v[reckey] === o) {
|
|
|
172 |
// delete v[reckey];
|
|
|
173 |
// }
|
|
|
174 |
//
|
|
|
175 |
// //add the new updated record if it has a key that corresponds to a hash table
|
|
|
176 |
// if (o.getValue(key)) {
|
|
|
177 |
// v[o.getValue(key)] = o;
|
|
|
178 |
// }
|
|
|
179 |
|
|
|
180 |
});
|
|
|
181 |
});
|
|
|
182 |
*/
|
|
|
183 |
},
|
|
|
184 |
|
|
|
185 |
//---------------------------------------------
|
|
|
186 |
// Public Methods
|
|
|
187 |
//---------------------------------------------
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* @description Creates a new hash table.
|
|
|
191 |
*
|
|
|
192 |
* @method createTable
|
|
|
193 |
* @param key {string} A key to hash by.
|
|
|
194 |
* @return tbls[key] {object} The created hash table
|
|
|
195 |
* @public
|
|
|
196 |
*/
|
|
|
197 |
createTable: function(key) {
|
|
|
198 |
var tbls = this.get('hashTables');
|
|
|
199 |
tbls[key] = this._setHashTable(key);
|
|
|
200 |
this.set('hashTables', tbls);
|
|
|
201 |
|
|
|
202 |
return tbls[key];
|
|
|
203 |
},
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* @description Get a hash table that hashes records by a given key.
|
|
|
208 |
*
|
|
|
209 |
* @method getTable
|
|
|
210 |
* @param key {string} A key to hash by.
|
|
|
211 |
* @return table {object} The created hash table
|
|
|
212 |
* @public
|
|
|
213 |
*/
|
|
|
214 |
getTable: function(key) {
|
|
|
215 |
return this.get('hashTables')[key];
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
});
|
|
|
223 |
Y.namespace("Plugin").RecordsetIndexer = RecordsetIndexer;
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
}, '3.18.1', {"requires": ["recordset-base", "plugin"]});
|