Merge pull request #363 from nasa/open302

[Representation] Check full path when comparing domain objects
This commit is contained in:
akhenry 2015-12-03 10:01:35 -08:00
commit 983973843e
2 changed files with 71 additions and 7 deletions

View File

@ -96,7 +96,7 @@ define(
toClear = [], // Properties to clear out of scope on change
counter = 0,
couldRepresent = false,
lastId,
lastIdPath = [],
lastKey,
changeTemplate = templateLinker.link($scope, element);
@ -143,11 +143,27 @@ define(
});
}
function unchanged(canRepresent, id, key) {
function unchanged(canRepresent, idPath, key) {
return canRepresent &&
couldRepresent &&
id === lastId &&
key === lastKey;
key === lastKey &&
idPath.length === lastIdPath.length &&
idPath.every(function (id, i) {
return id === lastIdPath[i];
});
}
function getIdPath(domainObject) {
if (!domainObject) {
return [];
}
if (!domainObject.hasCapability('context')) {
return [domainObject.getId()];
}
return domainObject.getCapability('context')
.getPath().map(function (pathObject) {
return pathObject.getId();
});
}
// General-purpose refresh mechanism; should set up the scope
@ -159,10 +175,10 @@ define(
path = representation && getPath(representation),
uses = ((representation || {}).uses || []),
canRepresent = !!(path && domainObject),
id = domainObject && domainObject.getId(),
idPath = getIdPath(domainObject),
key = $scope.key;
if (unchanged(canRepresent, id, key)) {
if (unchanged(canRepresent, idPath, key)) {
return;
}
@ -190,7 +206,7 @@ define(
// To allow simplified change detection next time around
couldRepresent = canRepresent;
lastId = id;
lastIdPath = idPath;
lastKey = key;
// Populate scope with fields associated with the current

View File

@ -247,6 +247,54 @@ define(
mockScope.$watch.calls[0].args[1]();
expect(mockScope.testCapability).toBeUndefined();
});
it("detects changes among linked instances", function () {
var mockContext = jasmine.createSpyObj('context', ['getPath']),
mockContext2 = jasmine.createSpyObj('context', ['getPath']),
mockLink = jasmine.createSpyObj(
'linkedObject',
DOMAIN_OBJECT_METHODS
),
mockParent = jasmine.createSpyObj(
'parentObject',
DOMAIN_OBJECT_METHODS
),
callCount;
mockDomainObject.getCapability.andCallFake(function (c) {
return c === 'context' && mockContext;
});
mockLink.getCapability.andCallFake(function (c) {
return c === 'context' && mockContext2;
});
mockDomainObject.hasCapability.andCallFake(function (c) {
return c === 'context';
});
mockLink.hasCapability.andCallFake(function (c) {
return c === 'context';
});
mockLink.getModel.andReturn({});
mockContext.getPath.andReturn([mockDomainObject]);
mockContext2.getPath.andReturn([mockParent, mockLink]);
mockLink.getId.andReturn('test-id');
mockDomainObject.getId.andReturn('test-id');
mockParent.getId.andReturn('parent-id');
mockScope.key = "abc";
mockScope.domainObject = mockDomainObject;
mockScope.$watch.calls[0].args[1]();
callCount = mockChangeTemplate.calls.length;
mockScope.domainObject = mockLink;
mockScope.$watch.calls[0].args[1]();
expect(mockChangeTemplate.calls.length)
.toEqual(callCount + 1);
});
});
}
);