Skip to content

Commit

Permalink
Addressed Issue SteveSanderson#45, added 2 test cases.
Browse files Browse the repository at this point in the history
Inner mapping options were not being respected
  • Loading branch information
bryansquared committed Feb 14, 2012
1 parent 0eb8d65 commit ba78343
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
12 changes: 9 additions & 3 deletions knockout.mapping.js
Expand Up @@ -615,15 +615,21 @@ ko.exportProperty = function (owner, publicName, object) {

var parentName = options.parentName;
visitPropertiesOrArrayEntries(unwrappedRootObject, function (indexer) {
if (options.ignore && ko.utils.arrayIndexOf(options.ignore, indexer) != -1) return;
var currentOptions = options;
var mappingObject = unwrappedRootObject[mappingProperty];
if (mappingObject) {
currentOptions = fillOptions(mappingObject,options);
}

if (currentOptions.ignore && ko.utils.arrayIndexOf(currentOptions.ignore, indexer) != -1) return;

var propertyValue = unwrappedRootObject[indexer];
options.parentName = getPropertyName(parentName, unwrappedRootObject, indexer);

// If we don't want to explicitly copy the unmapped property...
if (ko.utils.arrayIndexOf(options.copy, indexer) === -1) {
if (ko.utils.arrayIndexOf(currentOptions.copy, indexer) === -1) {
// ...find out if it's a property we want to explicitly include
if (ko.utils.arrayIndexOf(options.include, indexer) === -1) {
if (ko.utils.arrayIndexOf(currentOptions.include, indexer) === -1) {
// The mapped properties object contains all the properties that were part of the original object.
// If a property does not exist, and it is not because it is part of an array (e.g. "myProp[3]"), then it should not be unmapped.
if (unwrappedRootObject[mappingProperty] && unwrappedRootObject[mappingProperty].mappedProperties && !unwrappedRootObject[mappingProperty].mappedProperties[indexer] && !(unwrappedRootObject instanceof Array)) {
Expand Down
40 changes: 40 additions & 0 deletions spec/mappingBehaviors.js
Expand Up @@ -1549,3 +1549,43 @@ test('ko.mapping.fromJS should properly map objects that appear in multiple plac
equal(y.x[0].title, "Lorem ipsum");
equal(z.x()[0].title(), "Lorem ipsum");
});

test('ko.mapping.fromJS should honor inner mapping copy options', function() {
var obj = { title: "Lorem ipsum",
items: [{soup:1,bread:2}]};

var mapping = {
items: {
create: function(options) {
return ko.mapping.fromJS(options.data,{copy:['soup']});
}
}
}
a = ko.mapping.fromJS(obj,mapping);
b = ko.mapping.toJS(a);
equal(a.items()[0].soup,1);
equal(a.items()[0].bread(),2);
equal(b.items[0].soup,1);
equal(b.items[0].bread,2);
});

test('ko.mapping.fromJS should honor inner mapping ignore options', function() {
var obj = { title: "Lorem ipsum",
items: [{soup:1,bread:2}]};

var mapping = {
items: {
create: function(options) {
return ko.mapping.fromJS(options.data,{ignore:['soup']});
}
}
}
a = ko.mapping.fromJS(obj,mapping);
b = ko.mapping.toJS(a);
equal(a.items()[0].soup,undefined);
equal(a.items()[0].bread(),2);
equal(b.items[0].soup,undefined);
equal(b.items[0].bread,2);
});


0 comments on commit ba78343

Please sign in to comment.