Skip to content

Instantly share code, notes, and snippets.

@OndeVai
Created April 16, 2012 16:27
Show Gist options
  • Save OndeVai/2399789 to your computer and use it in GitHub Desktop.
Save OndeVai/2399789 to your computer and use it in GitHub Desktop.
KnockoutJS Make Json Data Observable (lighter mapping plugin)
//todo make this a plugin for ko
function makeAllPropsObservable(jsonData) {
////console.log(jsonData.DivisionSummaries);
for (x in jsonData) {
////console.log(x + ' ' + jsonData[x]);
if (!$.isFunction(jsonData[x])) {
if (!$.isArray(jsonData[x])) {
if (!$.isPlainObject(jsonData[x]))
jsonData[x] = ko.observable(jsonData[x]);
else {
var plainObj = jsonData[x];
makeAllPropsObservable(plainObj);
}
////console.log(x + ' ' + jsonData[x]());
}
else {
jsonData[x] = ko.observableArray(jsonData[x]);
var arr = jsonData[x];
for (var i = 0; i < arr().length; i++) {
makeAllPropsObservable(arr[i]);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment