Created
February 27, 2012 22:35
-
-
Save domenic/1927620 to your computer and use it in GitHub Desktop.
Attempt at an `optionsHash` binding for Knockout.js
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.optionsHash = { | |
update: function (element, valueAccessor, allBindingsAccessor) { | |
var hash = ko.utils.unwrapObservable(valueAccessor()); | |
var asArray = Object.keys(hash).map(function (k) { | |
return { | |
key: k, | |
val: hash[k] | |
}; | |
}); | |
function maskedAllBindingsAccessor() { | |
return ko.utils.extend(allBindingsAccessor(), { optionsValue: "key", optionsText: "val" }); | |
} | |
function maskedValueAccessor() { | |
return asArray; | |
} | |
return ko.bindingHandlers.options.update(element, maskedValueAccessor, maskedAllBindingsAccessor); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment