Merge pull request #4594 from furinvader/refactoring/hal/hal-link

Refactor HalLink
pull/4598/head
Alex Dik 8 years ago committed by GitHub
commit 29cde57927
  1. 37
      frontend/app/components/api/api-v3/hal-link/hal-link.service.test.ts
  2. 16
      frontend/app/components/api/api-v3/hal-link/hal-link.service.ts
  3. 4
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts
  4. 2
      frontend/app/components/wp-edit/field-types/wp-edit-wiki-textarea-field.module.ts
  5. 1
      frontend/app/typings/open-project.typings.ts

@ -166,14 +166,14 @@ describe('HalLink service', () => {
});
describe('when using the link function wrapper', () => {
describe('when making the link callable', () => {
var func;
const runChecks = () => {
it('should return a function that fetches the data', () => {
func();
$httpBackend.expectGET('/api/link').respond(200);
$httpBackend.flush()
$httpBackend.expectPOST('foo').respond(200);
$httpBackend.flush();
});
it('should pass the params to $fetch', () => {
@ -182,18 +182,41 @@ describe('HalLink service', () => {
expect($fetch.calledWith('hello')).to.be.true;
});
it('should have the href property of the link', () => {
expect(func.href).to.equal(link.href);
});
it('should have the title property of the link', () => {
expect(func.title).to.equal(link.title);
});
it('should have the method property of the link', () => {
expect(func.method).to.equal(link.method);
});
it('should have the templated property of the link', () => {
expect(func.templated).to.equal(link.templated);
});
};
describe('when using $toFunc', () => {
beforeEach(() => {
link.href = 'foo';
link.title = 'title';
link.method = 'post';
link.templated = true;
});
describe('when using the instance method', () => {
beforeEach(() => {
func = link.$toFunc();
func = link.$callable();
});
runChecks();
});
describe('when using the static factory function', () => {
describe('when using the static factory method', () => {
beforeEach(() => {
func = HalLink.asFunc(link);
func = HalLink.callable(link);
link = func.$link;
});
runChecks();

@ -43,8 +43,8 @@ export class HalLink implements HalLinkInterface {
return new HalLink(link.href, link.title, link.method, link.templated);
}
public static asFunc(link) {
return HalLink.fromObject(link).$toFunc();
public static callable(link) {
return HalLink.fromObject(link).$callable();
}
/**
@ -72,10 +72,16 @@ export class HalLink implements HalLinkInterface {
return this.$route[this.method === 'delete' && 'remove' || this.method](...params);
}
public $toFunc() {
public $callable() {
const func:any = (...params) => this.$fetch(...params);
func.$link = this;
func.$route = this.$route;
_.extend(func, {
$link: this,
href: this.href,
title: this.title,
method: this.method,
templated: this.templated
});
return func;
}

@ -184,7 +184,7 @@ function initializeResource(halResource:HalResource) {
if (link.href) {
if (link.method !== 'get') {
return HalLink.asFunc(link);
return HalLink.callable(link);
}
return createLinkedResource(linkName, link);
@ -218,7 +218,7 @@ function initializeResource(halResource:HalResource) {
function setupLinks() {
setupProperty('links',
link => Array.isArray(link) ? link.map(HalLink.asFunc) : HalLink.asFunc(link));
link => Array.isArray(link) ? link.map(HalLink.callable) : HalLink.callable(link));
}
function setupEmbedded() {

@ -82,7 +82,7 @@ export class WikiTextareaEditField extends EditField {
if (this.isPreview) {
this.isBusy = true;
this.workPackage.getForm().then(form => {
var previewLink = form.$links.previewMarkup.$route;
var previewLink = form.$links.previewMarkup.$link.$route;
previewLink
.customPOST(this.fieldVal.raw, void 0, void 0, {'Content-Type': 'text/plain; charset=UTF-8'})
.then(result => {

@ -153,7 +153,6 @@ declare namespace api {
interface Function {
$link?:any;
$route?:restangular.IService;
name:string;
_type:string;
}

Loading…
Cancel
Save