From ede7f165bf5b8b1bf2e42bbc0f50474a2d4a8b6c Mon Sep 17 00:00:00 2001 From: Alex Dik Date: Thu, 3 Mar 2016 19:44:31 +0100 Subject: [PATCH] Provide a name attribute that uses the self link's title This is useful when the resource is not loaded, but the name attribute is needed. Usually the name attribute is the title attribute of the link. --- .../api-v3/hal/hal-resource.service.test.ts | 29 +++++++++++++++++-- .../api/api-v3/hal/hal-resource.service.ts | 10 +++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/frontend/app/components/api/api-v3/hal/hal-resource.service.test.ts b/frontend/app/components/api/api-v3/hal/hal-resource.service.test.ts index 507e164ab5..8c5ec5d787 100644 --- a/frontend/app/components/api/api-v3/hal/hal-resource.service.test.ts +++ b/frontend/app/components/api/api-v3/hal/hal-resource.service.test.ts @@ -88,6 +88,7 @@ describe('halTransform service', () => { }, self: { href: '/api/v3/hello', + title: 'some title' } } }; @@ -103,14 +104,36 @@ describe('halTransform service', () => { expect(transformedElement.$halTransformed).to.be.true; }); - describe('when returning back the plain object', () => { + describe('when the self link has a title attribute', () => { + beforeEach(() => { + transformedElement = halTransform({ + _links: { + self: { + href: '/api/hello', + title: 'some title' + } + } + }); + }); + + it('should have a name attribute that is equal to the title of the self link', () => { + expect(transformedElement.name).to.eq('some title'); + }); + + it('should have a writable name attribute', () => { + transformedElement.name = 'some name'; + expect(transformedElement.name).to.eq('some name'); + }); + }); + + //TODO: Fix + describe.skip('when returning back the plain object', () => { var element; beforeEach(() => { element = transformedElement.$plain(); }); - //TODO: Fix - it.skip('should be the same as the source element', () => { + it('should be the same as the source element', () => { expect(element).to.eql(plainElement); }); }); diff --git a/frontend/app/components/api/api-v3/hal/hal-resource.service.ts b/frontend/app/components/api/api-v3/hal/hal-resource.service.ts index 8f1dfe54df..8c9bcac1b8 100644 --- a/frontend/app/components/api/api-v3/hal/hal-resource.service.ts +++ b/frontend/app/components/api/api-v3/hal/hal-resource.service.ts @@ -32,10 +32,20 @@ function halResource(halTransform, HalLink, $q) { public $embedded; public $halTransformed: boolean = true; + private _name:string; + protected static fromLink(link) { return new HalResource({_links: {self: link}}, false); } + public get name():string { + return this._name || this.$links.self.$link.title || ''; + } + + public set name(name:string) { + this._name = name; + } + constructor(protected $source, public $loaded = true) { var source = $source.restangularized ? $source.plain() : angular.copy($source);