updating KO model from json
This file contains 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
// 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