/addremoveitem.js Secret
Last active
August 29, 2015 13:55
Star
You must be signed in to star a gist
Implementing addItem and removeItem in the change tracked array extender
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
//.... | |
//.... | |
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