Skip to content

Instantly share code, notes, and snippets.

@mattparker
Created September 27, 2010 18:15
Show Gist options
  • Save mattparker/599527 to your computer and use it in GitHub Desktop.
Save mattparker/599527 to your computer and use it in GitHub Desktop.
// A rough idea for a RecordSetIndex plugin
// Filtering, searching, sorting on the host RecordSet could use the index if
// it's available (possibly creating it the first time someone does a sort or whatever)
// So RecordSet.filter() should be something like:
/*
filter: function(k,v) {
if (this.hasIndex(k)) {
return this.getIndex(k).find(v);
} else {
// proceed as normal!
}
}
*/
// Sorting would be similar, it'd just get the pre-sorted index
_index:{
},
initializer: function() {
var sorted = this.get("host").sort(this.get("sortFn")),
key = this.get("key"),
recVal;
sorted.each(function(r){
recVal = r.getValue(key);
if (this._index[recVal] === undefined) {
this._index[recVal] = [];
}
this._index[recVal].push(r); // Or perhaps this._index[key].push(r.get("id"));
});
},
find: function(from, to) {
return this._index[v];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment