Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github

beyond-code-github/addremoveitem.js Secret

Last active Aug 29, 2015
Embed
What would you like to do?
Implementing addItem and removeItem in the change tracked array extender
//....
//....
var findItem = function (array, item) {
return ko.utils.arrayFirst(array, function (o) {
return o === item;
});
};
var addItem = function (item) {
var previouslyRemoved = findItem(target.removed(), item);
if (previouslyRemoved) {
target.removed.remove(previouslyRemoved);
} else {
target.added.push(item);
target.isDirty(true);
}
};
var removeItem = function (item) {
var previouslyAdded = findItem(target.added(), item);
if (previouslyAdded) {
target.added.remove(previouslyAdded);
} else {
target.removed.push(item);
target.isDirty(true);
}
};
//....
//....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.