Skip to content

Instantly share code, notes, and snippets.

@fabianobizarro
Last active August 24, 2017 18:52
Show Gist options
  • Save fabianobizarro/ba27ff7d71c90d726c586a0f6555a45c to your computer and use it in GitHub Desktop.
Save fabianobizarro/ba27ff7d71c90d726c586a0f6555a45c to your computer and use it in GitHub Desktop.
var ObservableList = (function () {
function ObservableList() {
this.list = [];
this.events = [];
}
ObservableList.prototype.triggerEvent = function (eventName, data) {
var event = this.events[eventName];
if (event) {
event.forEach(function (callback) {
callback(data);
});
}
}
ObservableList.prototype.addListener = function (eventName, listener) {
if (this.events[eventName]) {
events[eventName].push(listener);
} else {
this.events[eventName] = [];
this.events[eventName].push(listener);
}
}
ObservableList.prototype.import = function (elementsArray) {
var that = this;
elementsArray.forEach(function (e) {
that.list.push(e);
});
}
ObservableList.prototype.add = function (value) {
this.list.push(value);
this.triggerEvent('elementInserted', { list: this.list, element: value });
}
ObservableList.prototype.onElementInserted = function (callback) {
this.addListener('elementInserted', callback);
}
ObservableList.prototype.onElementRemoved = function (callback) {
this.addListener('elementRemoved', callback);
}
ObservableList.prototype.onElementUpdated = function (callback) {
this.addListener('elementUpdated', callback);
}
ObservableList.prototype.delete = function (element) { }
ObservableList.prototype.getElement = function (element) { }
ObservableList.prototype.update = function (current, newElement) { }
return ObservableList;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment