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

Commit 0d9b49e

Browse files
committed
deletion of a vairous any types
1 parent e6a326f commit 0d9b49e

10 files changed

Lines changed: 108 additions & 87 deletions

File tree

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.5.5",
3+
"version": "0.5.12",
44
"description": "JSONAPI library developed for AngularJS in Typescript",
55
"repository": {
66
"type": "git",

src/library/core.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
1-
/// <reference path="./index.d.ts" />
2-
31
import './services/core-services.service';
4-
import { ICore, IResource } from './interfaces';
2+
import { ICore, IService } from './interfaces';
53

64
export class Core implements ICore {
7-
public rootPath: string = 'http://url/';
8-
public resources: Object = {};
5+
private resourceServices: Object = {};
96

107
public loadingsCounter: number = 0;
118
public loadingsStart = () => {};
129
public loadingsDone = () => {};
1310
public loadingsError = () => {};
1411
public loadingsOffline = () => {};
1512

16-
public static Me: ICore = null;
17-
public static Services: any = null;
13+
public static me: ICore;
14+
public static injectedServices: any;
1815

1916
/** @ngInject */
2017
public constructor(
2118
protected rsJsonapiConfig,
2219
protected JsonapiCoreServices
2320
) {
24-
Core.Me = this;
25-
Core.Services = JsonapiCoreServices;
21+
Core.me = this;
22+
Core.injectedServices = JsonapiCoreServices;
2623
}
2724

28-
public _register(clase: IResource): boolean {
29-
if (clase.type in this.resources) {
25+
public _register(clase: IService): boolean {
26+
if (clase.type in this.resourceServices) {
3027
return false;
3128
}
32-
this.resources[clase.type] = clase;
29+
this.resourceServices[clase.type] = clase;
3330
return true;
3431
}
3532

36-
public getResource(type: string): IResource {
37-
return this.resources[type];
33+
public getResourceService(type: string): IService {
34+
return this.resourceServices[type];
3835
}
3936

4037
public refreshLoadings(factor: number): void {
@@ -46,4 +43,5 @@ export class Core implements ICore {
4643
}
4744
}
4845
}
46+
4947
angular.module('Jsonapi.services').service('JsonapiCore', Core);

src/library/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference path="./index.d.ts" />
2-
31
import * as angular from 'angular';
42
import 'angular-localforage';
53

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export interface IAttributes {
2-
[value: string]: boolean | string | number;
2+
// problem with lines like
3+
/*
4+
this._receipt_number = order.attributes.receipt_number;
5+
this.transaction.attributes._due_date_with_format = new Date(this.transaction.attributes.due_date);
6+
*/
7+
// [value: string]: boolean | string | number;
8+
[value: string]: any;
39
}

src/library/interfaces/core.d.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import { IResource } from './resource';
1+
import { IService } from './index';
22

33
export interface ICore {
4-
rootPath?: string;
5-
resources?: Object;
4+
// jsonapiServices: Object;
65

7-
Me?: IResource;
8-
Services?: any;
6+
loadingsCounter: number;
7+
loadingsStart: Function;
8+
loadingsDone: Function;
9+
loadingsError: Function;
10+
loadingsOffline: Function;
911

10-
loadingsStart?: Function;
11-
loadingsDone?: Function;
12-
loadingsError?: Function;
13-
loadingsOffline?: Function;
12+
_register (clase: IService): boolean;
13+
getResourceService (type: string): IService;
14+
refreshLoadings (factor: number): void;
1415

15-
_register? (clase: any): boolean;
16-
getResource? (type: string): IResource;
17-
refreshLoadings?(factor: number): void;
16+
// static
17+
me?: IService;
18+
injectedServices?: any;
1819
}

src/library/parent-resource-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Base } from './services/base';
2-
import { IExecParams } from './interfaces';
2+
import { IResource, ICollection, IExecParams } from './interfaces';
33

44
export class ParentResourceService {
55
/**
66
This method sort params for all(), get(), delete() and save()
77
*/
8-
protected __exec(exec_params: IExecParams): any {
8+
protected __exec(exec_params: IExecParams): IResource | ICollection | void {
99
// makes `params` optional
1010
if (angular.isFunction(exec_params.params)) {
1111
exec_params.fc_error = exec_params.fc_success;

src/library/resource.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ export class Resource extends ParentResourceService implements IResource {
2121
public attributes: IAttributes = {};
2222
public relationships: IRelationships = {};
2323

24+
public reset(): void {
25+
let self = this;
26+
this.id = '';
27+
this.attributes = {};
28+
angular.forEach(this.getService().schema.attributes, (value, key) => {
29+
self.attributes[key] = ('default' in value) ? value.default : undefined;
30+
});
31+
this.relationships = {};
32+
angular.forEach(this.getService().schema.relationships, (value, key) => {
33+
self.relationships[key] = <IRelationship>{ data: {} };
34+
if (this.getService().schema.relationships[key].hasMany) {
35+
self.relationships[key].data = Base.newCollection();
36+
}
37+
});
38+
this.is_new = true;
39+
}
40+
2441
public toObject(params?: IParamsResource): IDataObject {
2542
params = angular.extend({}, Base.Params, params);
2643
// this.getService().schema = angular.extend({}, Base.Schema, this.getService().schema);
@@ -86,11 +103,11 @@ export class Resource extends ParentResourceService implements IResource {
86103
return ret;
87104
}
88105

89-
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): Array<T> {
90-
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
106+
public save<T extends IResource>(params?: Object | Function, fc_success?: Function, fc_error?: Function): T {
107+
return <T>this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'save' });
91108
}
92109

93-
protected __exec(exec_params: IExecParams): any {
110+
protected __exec(exec_params: IExecParams): IResource {
94111
super.__exec(exec_params);
95112

96113
switch (exec_params.exec_type) {
@@ -116,7 +133,7 @@ export class Resource extends ParentResourceService implements IResource {
116133

117134
let resource = Converter.newResource(this.type, this.id);
118135

119-
let promise = Core.Services.JsonapiHttp.exec(
136+
let promise = Core.injectedServices.JsonapiHttp.exec(
120137
path.get(), this.id ? 'PUT' : 'POST',
121138
object, !(angular.isFunction(fc_error))
122139
);

src/library/service.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ export class Service extends ParentResourceService implements IService {
2626
@return true if the resource don't exist and registered ok
2727
**/
2828
public register(): boolean {
29-
if (Core.Me === null) {
29+
if (Core.me === null) {
3030
throw 'Error: you are trying register --> ' + this.type + ' <-- before inject JsonapiCore somewhere, almost one time.';
3131
}
3232
// only when service is registered, not cloned object
3333
this.memorycache = new MemoryCache();
3434
this.schema = angular.extend({}, Base.Schema, this.schema);
35-
return Core.Me._register(this);
35+
return Core.me._register(this);
3636
}
3737

38-
public new<T extends IResource>(id: string = ''): T {
38+
public new<T extends IResource>(): T {
3939
let resource: IResource = new Resource();
4040
resource.type = this.type;
41-
resource.id = id;
41+
resource.reset();
4242
return <T>resource;
4343
}
4444

@@ -50,18 +50,18 @@ export class Service extends ParentResourceService implements IService {
5050
}
5151

5252
public get<T extends IResource>(id, params?: IParamsResource | Function, fc_success?: Function, fc_error?: Function): T {
53-
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'get' });
53+
return <T>this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'get' });
5454
}
5555

5656
public delete(id: string, params?: Object | Function, fc_success?: Function, fc_error?: Function): void {
57-
return this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'delete' });
57+
return <void>this.__exec({ id: id, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'delete' });
5858
}
5959

6060
public all(params?: IParamsCollection | Function, fc_success?: Function, fc_error?: Function): ICollection {
61-
return this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'all' });
61+
return <ICollection>this.__exec({ id: null, params: params, fc_success: fc_success, fc_error: fc_error, exec_type: 'all' });
6262
}
6363

64-
protected __exec(exec_params: IExecParams): any {
64+
protected __exec(exec_params: IExecParams): IResource | ICollection | void {
6565
super.__exec(exec_params);
6666

6767
switch (exec_params.exec_type) {
@@ -90,7 +90,7 @@ export class Service extends ParentResourceService implements IService {
9090
if (this.getService().memorycache.isResourceLive(id, temporal_ttl)) {
9191
// we create a promise because we need return collection before
9292
// run success client function
93-
var deferred = Core.Services.$q.defer();
93+
var deferred = Core.injectedServices.$q.defer();
9494
deferred.resolve(fc_success);
9595
deferred.promise.then(fc_success => {
9696
this.runFc(fc_success, 'memorycache');
@@ -100,7 +100,7 @@ export class Service extends ParentResourceService implements IService {
100100
}
101101

102102

103-
Core.Services.JsonapiHttp
103+
Core.injectedServices.JsonapiHttp
104104
.get(path.get())
105105
.then(
106106
success => {
@@ -134,9 +134,9 @@ export class Service extends ParentResourceService implements IService {
134134
params.remotefilter ? path.addParam(paramsurl.toparams( { filter: params.remotefilter } )) : null;
135135
if (params.page) {
136136
params.page.number > 1 ? path.addParam(
137-
Core.Services.rsJsonapiConfig.parameters.page.number + '=' + params.page.number) : null;
137+
Core.injectedServices.rsJsonapiConfig.parameters.page.number + '=' + params.page.number) : null;
138138
params.page.limit ? path.addParam(
139-
Core.Services.rsJsonapiConfig.parameters.page.limit + '=' + params.page.limit) : null;
139+
Core.injectedServices.rsJsonapiConfig.parameters.page.limit + '=' + params.page.limit) : null;
140140
}
141141

142142
// make request
@@ -162,7 +162,7 @@ export class Service extends ParentResourceService implements IService {
162162
if (this.getService().memorycache.isCollectionLive(path.getForCache(), temporal_ttl)) {
163163
// we create a promise because we need return collection before
164164
// run success client function
165-
var deferred = Core.Services.$q.defer();
165+
var deferred = Core.injectedServices.$q.defer();
166166
deferred.resolve(fc_success);
167167
deferred.promise.then(fc_success => {
168168
this.runFc(fc_success, 'memorycache');
@@ -174,7 +174,7 @@ export class Service extends ParentResourceService implements IService {
174174
tempororay_collection['$isloading'] = true;
175175

176176
// STORAGE_CACHE
177-
Core.Services.JsonapiHttpStorage
177+
Core.injectedServices.JsonapiHttpStorage
178178
.get(path.getForCache(), params.storage_ttl)
179179
.then(
180180
success => {
@@ -188,7 +188,7 @@ export class Service extends ParentResourceService implements IService {
188188

189189
this.runFc(fc_success, { data: success});
190190

191-
var deferred = Core.Services.$q.defer();
191+
var deferred = Core.injectedServices.$q.defer();
192192
deferred.resolve(fc_success);
193193
deferred.promise.then(fc_success => {
194194
this.runFc(fc_success, 'httpstorage');
@@ -205,7 +205,7 @@ export class Service extends ParentResourceService implements IService {
205205

206206
private getAllFromServer(path, params, fc_success, fc_error, tempororay_collection: ICollection) {
207207
// SERVER REQUEST
208-
Core.Services.JsonapiHttp
208+
Core.injectedServices.JsonapiHttp
209209
.get(path.get())
210210
.then(
211211
success => {
@@ -217,7 +217,7 @@ export class Service extends ParentResourceService implements IService {
217217
this.getService().memorycache.setCollection(path.getForCache(), tempororay_collection);
218218

219219
if (params.storage_ttl > 0) {
220-
Core.Services.JsonapiHttpStorage.save(path.getForCache(), success.data);
220+
Core.injectedServices.JsonapiHttpStorage.save(path.getForCache(), success.data);
221221
}
222222

223223
// localfilter getted data
@@ -251,7 +251,7 @@ export class Service extends ParentResourceService implements IService {
251251
path.appendPath(this.getPath());
252252
path.appendPath(id);
253253

254-
Core.Services.JsonapiHttp
254+
Core.injectedServices.JsonapiHttp
255255
.delete(path.get())
256256
.then(
257257
success => {
@@ -268,7 +268,7 @@ export class Service extends ParentResourceService implements IService {
268268
@return This resource like a service
269269
**/
270270
public getService<T extends IService>(): T {
271-
return Converter.getService(this.type);
271+
return <T>Converter.getService(this.type);
272272
}
273273

274274
public clearMemoryCache(): boolean {

src/library/services/http.service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { IDataObject } from '../interfaces/data-object';
33

44
import './noduplicatedhttpcalls.service';
55
import { Core } from '../core';
6-
// import { Resource } from '../resource';
76

87
export class Http {
98

@@ -52,24 +51,24 @@ export class Http {
5251

5352
let deferred = this.$q.defer();
5453
let self = this;
55-
Core.Me.refreshLoadings(1);
54+
Core.me.refreshLoadings(1);
5655
fakeHttpPromise.then(
5756
success => {
5857
// timeout just for develop environment
5958
self.$timeout( () => {
60-
Core.Me.refreshLoadings(-1);
59+
Core.me.refreshLoadings(-1);
6160
deferred.resolve(success);
6261
}, self.rsJsonapiConfig.delay);
6362
},
6463
error => {
65-
Core.Me.refreshLoadings(-1);
64+
Core.me.refreshLoadings(-1);
6665
if (error.status <= 0) {
6766
// offline?
68-
if (!Core.Me.loadingsOffline(error)) {
67+
if (!Core.me.loadingsOffline(error)) {
6968
console.warn('Jsonapi.Http.exec (use JsonapiCore.loadingsOffline for catch it) error =>', error);
7069
}
7170
} else {
72-
if (call_loadings_error && !Core.Me.loadingsError(error)) {
71+
if (call_loadings_error && !Core.me.loadingsError(error)) {
7372
console.warn('Jsonapi.Http.exec (use JsonapiCore.loadingsError for catch it) error =>', error);
7473
}
7574
}

0 commit comments

Comments
 (0)