Created
October 24, 2014 10:09
-
-
Save d0minicw0ng/dfd2ebb129a02f32b8e6 to your computer and use it in GitHub Desktop.
Knockout.js custom foreach binding that filters objects that have been destroyed. (The rule can be customized or possibly passed in)
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
ko.bindingHandlers.filterDestroyed = | |
init: (element, valueAccessor, allBindings, viewModel, bindingContext) -> | |
filteredValueAccessor = -> | |
observableArray = valueAccessor() | |
_.filter observableArray(), (viewModel) -> | |
viewModel._destroy() is false | |
ko.bindingHandlers["template"]["init"] element, ko.bindingHandlers["foreach"].makeTemplateValueAccessor(filteredValueAccessor) | |
update: (element, valueAccessor, allBindings, viewModel, bindingContext) -> | |
filteredValueAccessor = -> | |
observableArray = valueAccessor() | |
_.filter observableArray(), (viewModel) -> | |
viewModel._destroy() is false | |
ko.bindingHandlers["template"]["update"] element, ko.bindingHandlers["foreach"].makeTemplateValueAccessor(filteredValueAccessor), allBindings, viewModel, bindingContext | |
ko.virtualElements.allowedBindings.filterDestroyed = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment