Make property links lazy

pull/4238/head
Alex Dik 9 years ago
parent 9ed1a93101
commit 80dbbe4577
  1. 8
      frontend/app/components/api/api-v3/hal/hal-resource.service.test.ts
  2. 58
      frontend/app/components/api/api-v3/hal/hal-resource.service.ts

@ -388,6 +388,9 @@ describe('HalResource service', () => {
action: {
href: '/api/action',
method: 'post'
},
self: {
href: '/api/self'
}
},
_embedded: {
@ -473,6 +476,11 @@ describe('HalResource service', () => {
expect(resource.embedded.$loaded).to.be.true;
});
it('should update the source when set', () => {
resource.property = resource;
expect(resource.$source._links.property.href).to.eql('/api/self')
});
describe('when loading it', () => {
beforeEach(() => {
resource = resource.property;

@ -35,7 +35,7 @@ const lazy = (obj:any, property:string, callback:Function, setter:boolean = fals
}
return value;
},
set: void 0,
set: void 0
};
if (setter) {
@ -88,22 +88,7 @@ function halResource(halTransform, HalLink, $q) {
this.$source = $source._plain || angular.copy($source);
this.proxyProperties();
angular.forEach(this.$links, (link, name:string) => {
if (Array.isArray(link)) {
this[name] = link.map(HalResource.fromLink);
return;
}
link = link.$link;
if (link.href && link.method == 'get' && name !== 'self') {
this[name] = HalResource.fromLink(link);
}
else if (link.method !== 'get') {
this[name] = link.$toFunc();
}
});
this.setLinksAsProperties();
angular.forEach(this.$embedded, (resource, name) => {
this[name] = resource;
@ -185,6 +170,45 @@ function halResource(halTransform, HalLink, $q) {
});
});
}
private setLinksAsProperties() {
_.without(Object.keys(this.$links), 'self').forEach(linkName => {
var value;
const config = {
get() {
let link = this.$links[linkName].$link || this.$links[linkName];
if (!value) {
if (Array.isArray(link)) {
value = link.map(HalResource.fromLink);
}
if (link.href) {
if (link.method !== 'get') {
value = HalLink.asFunc(link);
}
else {
value = HalResource.fromLink(link);
}
}
}
return value;
},
set(val) {
let link = this.$links[linkName].$link;
if (link.href && link.method === 'get') {
value = val;
this.$source._links[linkName] = val.$links.self.$link;
}
}
};
Object.defineProperty(this, linkName, config);
});
}
}
}

Loading…
Cancel
Save