Skip to content

Instantly share code, notes, and snippets.

@matthewnitschke
Created July 9, 2016 02:37
Show Gist options
  • Select an option

  • Save matthewnitschke/3ebd133138eafaacb06d7f1576533d2a to your computer and use it in GitHub Desktop.

Select an option

Save matthewnitschke/3ebd133138eafaacb06d7f1576533d2a to your computer and use it in GitHub Desktop.
Simple methods to force a variable to an observable. This is useful so when calling these variables you can just use: variableName()
function observifyArray(arr){
for(var i = 0; i < ko.unwrap(arr).length; i ++){
ko.unwrap(arr)[i] = observify(ko.unwrap(arr)[i]);
}
if (ko.isObservable(arr) && 'push' in arr){
return arr;
} else {
return ko.observableArray(arr);
}
}
function observify(value) {
if (ko.isObservable(value)) {
return value;
} else {
return ko.observable(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment