Skip to content

Instantly share code, notes, and snippets.

@mcintyre321
Created May 7, 2014 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcintyre321/4a28cebfd8c07c49f1ba to your computer and use it in GitHub Desktop.
Save mcintyre321/4a28cebfd8c07c49f1ba to your computer and use it in GitHub Desktop.
Knockout liveMap
Array.prototype.liveMap = function (map, cleanUp) {
var destination = [];
this.map(function(item) {
destination[destination.length] = map(item);
});
this.subscribe(function (changes) {
changes.forEach(function(change) {
if (change.status === 'added') {
var t2 = map(change.value);
destination.push(t2);
//} else if (change.status === "") {
// destination[change.index] = map(change.value);
} else if (change.status === "deleted") {
var destItem = destination[change.index];
destination.splice(change.index, 1);
if (cleanUp) cleanUp(destItem);
}
});
}, null, "arrayChange");
return destination;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment