Created
May 7, 2014 10:10
-
-
Save mcintyre321/4a28cebfd8c07c49f1ba to your computer and use it in GitHub Desktop.
Knockout liveMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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