Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
updating KO model from json
// map the keys in json to the properties on your model
var desiredKeys = {
"foo": "foo",
"bar": "bar"
Object.defineProperty(self, 'updateFromJson', {
enumerable: true,
value: function (data) {
.forEach(function (key) {
var modelKey = desiredKeys[key];
if (!modelKey) return;
if ( !_.isEqual(self[modelKey], data[key]) )
self[modelKey] = data[key]; // I can use = syntax because I use Object.defineproperty with setters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment