Created
June 1, 2017 12:59
-
-
Save bh3605/a5e130f1a15dab8bac70815e812530c6 to your computer and use it in GitHub Desktop.
Helpful js functions for knockout.js library
This file contains hidden or 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
//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