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) { | |
Object.keys(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