Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ignore callback support #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion knockout.mapping.js
Expand Up @@ -24,6 +24,7 @@
var _defaultOptions = {
include: ["_destroy"],
ignore: [],
ignoreCallback: null,
copy: [],
observe: []
};
Expand Down Expand Up @@ -162,6 +163,7 @@

if (arguments.length == 0) throw new Error("When calling ko.mapping.toJS, pass the object you want to convert.");
if (exports.getType(defaultOptions.ignore) !== "array") throw new Error("ko.mapping.defaultOptions().ignore should be an array.");
if (defaultOptions.ignoreCallback != null && exports.getType(defaultOptions.ignoreCallback) !== "function") throw new Error("ko.mapping.defaultOptions().ignoreCallback should be a function.");
if (exports.getType(defaultOptions.include) !== "array") throw new Error("ko.mapping.defaultOptions().include should be an array.");
if (exports.getType(defaultOptions.copy) !== "array") throw new Error("ko.mapping.defaultOptions().copy should be an array.");

Expand Down Expand Up @@ -191,7 +193,8 @@
defaultOptions = {
include: _defaultOptions.include.slice(0),
ignore: _defaultOptions.ignore.slice(0),
copy: _defaultOptions.copy.slice(0)
copy: _defaultOptions.copy.slice(0),
ignoreCallback: _defaultOptions.ignoreCallback
};
};

Expand Down Expand Up @@ -224,11 +227,13 @@
options.include = mergeArrays(otherOptions.include, options.include);
options.copy = mergeArrays(otherOptions.copy, options.copy);
options.observe = mergeArrays(otherOptions.observe, options.observe);
options.ignoreCallback = options.ignoreCallback ? options.ignoreCallback : otherOptions.ignoreCallback;
}
options.ignore = mergeArrays(options.ignore, defaultOptions.ignore);
options.include = mergeArrays(options.include, defaultOptions.include);
options.copy = mergeArrays(options.copy, defaultOptions.copy);
options.observe = mergeArrays(options.observe, defaultOptions.observe);
options.ignoreCallback = options.ignoreCallback ? options.ignoreCallback : defaultOptions.ignoreCallback;

options.mappedProperties = options.mappedProperties || {};
options.copiedProperties = options.copiedProperties || {};
Expand Down Expand Up @@ -450,6 +455,9 @@
return;
}

if (options.ignoreCallback != null && options.ignoreCallback(fullPropertyName)) {
return;
}
if (ko.utils.arrayIndexOf(options.copy, fullPropertyName) != -1) {
mappedRootObject[indexer] = rootObject[indexer];
return;
Expand Down Expand Up @@ -733,6 +741,7 @@
visitPropertiesOrArrayEntries(unwrappedRootObject, function (indexer) {
if (options.ignore && ko.utils.arrayIndexOf(options.ignore, indexer) != -1) return;

if (options.ignoreCallback != null && options.ignoreCallback(indexer)) return;
var propertyValue = unwrappedRootObject[indexer];
options.parentName = getPropertyName(parentName, unwrappedRootObject, indexer);

Expand Down