Skip to content
This repository was archived by the owner on Aug 18, 2018. It is now read-only.

Commit 85318db

Browse files
committed
JsonapiCore.duplicateResource() adeded
1 parent e9747bc commit 85318db

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Save on localstore all data. When you request a resource or collection, first check memory. If its empty, read from store. If is empty, get the data from back-end.
1111
- HttpStorage deprecated: jsons were saved as sent by the server, now we save json with logic (saving ids and resources separately).
1212
- Service with `toServer()` and `fromServer()` functions. They execute before and after http request. Ideal for type conversions.
13+
- `JsonapiCore.duplicateResource(resouce, ...relationtypes)` return a duplication of resource. You can duplicate resources and, optionally, their relationships. (v0.6.16)
1314

1415
## No more declaration file .d.ts
1516

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-angular-jsonapi",
3-
"version": "0.6.15",
3+
"version": "0.6.16",
44
"description": "JSONAPI library developed for AngularJS in Typescript",
55
"repository": {
66
"type": "git",

src/demo/authors/authors.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class AuthorsController {
55

66
/** @ngInject */
77
constructor(
8-
protected JsonapiCore,
8+
protected JsonapiCore: Jsonapi.ICore,
99
protected AuthorsService: Jsonapi.IService
1010
) {
1111
this.authors = AuthorsService.all(

src/library/core.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as angular from 'angular';
22
import './services/core-services.service';
3-
import { ICore, IService } from './interfaces';
3+
import { ICore, IResource, ICollection, IService } from './interfaces';
44

55
export class Core implements ICore {
66
private resourceServices: Object = {};
@@ -48,6 +48,33 @@ export class Core implements ICore {
4848
Core.injectedServices.JsonapiStoreService.clearCache();
4949
return true;
5050
}
51+
52+
// just an helper
53+
public duplicateResource(resource: IResource, ...relations_alias_to_duplicate_too: Array<string>): IResource {
54+
let newresource = this.getResourceService(resource.type).new();
55+
angular.merge(newresource.attributes, resource.attributes);
56+
newresource.attributes.name = newresource.attributes.name + ' xXx';
57+
angular.forEach(resource.relationships, (relationship, alias) => {
58+
if ('id' in relationship.data) {
59+
// relation hasOne
60+
if (alias in relations_alias_to_duplicate_too) {
61+
newresource.addRelationship(this.duplicateResource(<IResource>relationship.data), alias);
62+
} else {
63+
newresource.addRelationship(<IResource>relationship.data, alias);
64+
}
65+
} else {
66+
// relation hasMany
67+
if (alias in relations_alias_to_duplicate_too) {
68+
angular.forEach(relationship.data, relationresource => {
69+
newresource.addRelationship(this.duplicateResource(relationresource), alias);
70+
});
71+
} else {
72+
newresource.addRelationships(<ICollection>relationship.data, alias);
73+
}
74+
}
75+
});
76+
return newresource;
77+
}
5178
}
5279

5380
angular.module('Jsonapi.services').service('JsonapiCore', Core);

src/library/interfaces/core.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IService } from './index';
1+
import { IService, IResource } from './index';
22

33
export interface ICore {
44
// jsonapiServices: Object;
@@ -13,6 +13,7 @@ export interface ICore {
1313
getResourceService(type: string): IService;
1414
refreshLoadings(factor: number): void;
1515
clearCache(): void;
16+
duplicateResource(resource: IResource, ...relations_types: Array<string>): IResource;
1617

1718
// static
1819
me?: IService;

0 commit comments

Comments
 (0)