Skip to content

Instantly share code, notes, and snippets.

@petermichaux
Created February 14, 2012 20:53
Show Gist options
  • Save petermichaux/1830345 to your computer and use it in GitHub Desktop.
Save petermichaux/1830345 to your computer and use it in GitHub Desktop.
using constructor
function Set() {
this._elements = [];
}
Set.prototype.filter = function(fn) {
var result = new this.constructor();
for (var i = 0; i < this._elements.length; i++) {
if (fn(this._elements[i]) {
result.add(this._elements[i]);
}
}
};
// ObservableSet inherits from Set
//
// Note that calling filter on an observable set will
// return an observable set.
//
function ObservableSet() {
Set.apply(this, arguments);
}
// mixin the addEventListener, removeEventListener, dispatchEvent methods.
mixinObservable(ObservableSet.prototype);
ObservableSet.prototype.add = function(element) {
var modified = Set.prototype.add.call(this, element);
if (modifed) {
this.dispatchEvent({type:'add', relatedTarget: element});
}
return modified;
};
ObservableSet.prototype['delete'] = function(element) {
var modified = Set.prototype['delete'].call(this, element);
if (modifed) {
this.dispatchEvent({type:'delete', relatedTarget: element});
}
return modified;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment