Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active August 29, 2015 13:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save beyond-code-github/96471c3e4d6feed349c1 to your computer and use it in GitHub Desktop.
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