Skip to content

Instantly share code, notes, and snippets.

@beyond-code-github
Last active January 3, 2016 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beyond-code-github/8385771 to your computer and use it in GitHub Desktop.
Save beyond-code-github/8385771 to your computer and use it in GitHub Desktop.
Changes required to trackChange and getChangesFromModel methods to support change tracking as well as detection
ko.extenders.trackChange = function (target, track) {
if (track) {
// ...
// ...
if (!target.getChanges) {
target.getChanges = function (newObject) {
var obj = target();
if ((typeof obj == "object") && (obj !== null)) {
if (target.hasValueChanged()) {
return ko.mapping.toJS(obj);
}
return getChangesFromModel(obj);
}
return target();
};
}
}
return target;
};
var getChangesFromModel = function (obj) {
var changes = null;
var properties = getObjProperties(obj);
ko.utils.arrayForEach(properties, function (property) {
if (property.value != null && typeof property.value.isDirty != "undefined" && property.value.isDirty()) {
changes = changes || {};
changes[property.name] = property.value.getChanges();
}
});
return changes;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment