Created
July 9, 2016 02:37
-
-
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()
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
| 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