Skip to content

Instantly share code, notes, and snippets.

@bh3605
Created June 1, 2017 12:59
Show Gist options
  • Save bh3605/a5e130f1a15dab8bac70815e812530c6 to your computer and use it in GitHub Desktop.
Save bh3605/a5e130f1a15dab8bac70815e812530c6 to your computer and use it in GitHub Desktop.
Helpful js functions for knockout.js library
//given an object and a white list; reset all knockout observables to undefined
var reset = function (obj, whitelist) {
for (var prop in obj) {
if ( obj.hasOwnProperty(prop) && ko.isObservable(obj[prop]) && !ko.isComputed(obj[prop]) && whitelist.indexOf(prop) === -1 ) {
obj[prop](undefined);
}
}
};
//useful for when an object is used for the value property of a dropdown. Usage: "optionsAfterRender: setOptionValue('propertyName')"
var setOptionValue = function(propId){
return function(option, item) {
if(item === undefined) {
option.value = '';
}
else {
if(typeof(item[propId]) === typeof(function() {})) {
option.value = item[propId]();
}
else{
option.value = item[propId];
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment