@@ -38,10 +38,10 @@ export class Converter {
3838 static json_array2resources_array_by_type (
3939 json_array : Array < IDataResource > ,
4040 instance_relationships : boolean
41- ) : Object { // Array<Jsonapi.IResource> {
41+ ) : Array < Jsonapi . IResource > {
4242 let all_resources :any = { } ;
4343 Converter . json_array2resources_array ( json_array , all_resources , false ) ;
44- let resources = { } ;
44+ let resources = [ ] ;
4545 angular . forEach ( all_resources , ( resource ) => {
4646 if ( ! ( resource . type in resources ) ) {
4747 resources [ resource . type ] = { } ;
@@ -69,17 +69,37 @@ export class Converter {
6969 let resource_service = Core . Me . getResource ( type ) ;
7070 if ( angular . isUndefined ( resource_service ) ) {
7171 console . warn ( '`' + type + '`' , 'service not found on getService()' ) ;
72+ // resource_service = new Resource();
73+ // resource_service.memorycache = new MemoryCache();
74+ // resource_service.reset();
7275 }
7376 return resource_service ;
7477 }
7578
79+ static newResource ( type : string , id : string ) : Jsonapi . IResource {
80+ if ( Converter . getService ( type ) . memorycache && id in Converter . getService ( type ) . memorycache . resources ) {
81+ return Converter . getService ( type ) . memorycache . resources [ id ] ;
82+ } else {
83+ return Converter . getService ( type ) . new ( ) ;
84+ }
85+ }
86+
7687 /* return a resource type(resoruce_service) with data(data) */
7788 static procreate ( resource_service : Jsonapi . IResource , data : IDataResource ) : Jsonapi . IResource {
7889 if ( ! ( 'type' in data && 'id' in data ) ) {
7990 console . error ( 'Jsonapi Resource is not correct' , data ) ;
8091 }
81- let resource = new ( < any > resource_service . constructor ) ( ) ;
82- resource . new ( ) ;
92+
93+ let resource : Jsonapi . IResource ;
94+ if ( data . id in Converter . getService ( data . type ) . memorycache . resources ) {
95+ console . log ( 'HAY CACHE!' , data . type ) ;
96+ resource = Converter . getService ( data . type ) . memorycache . resources [ data . id ] ;
97+ } else {
98+ // resource = new (<any>resource_service.constructor)();
99+ // resource.new();
100+ resource = Converter . getService ( data . type ) . clone ( ) ;
101+ }
102+
83103 resource . id = data . id ;
84104 resource . attributes = data . attributes ? data . attributes : { } ;
85105 resource . is_new = false ;
@@ -92,68 +112,66 @@ export class Converter {
92112 schema : Jsonapi . ISchema
93113 ) {
94114 // instancio los include y los guardo en included arrary
95- let included_resources = { } ;
115+ let included_resources = [ ] ;
96116 if ( 'included' in document_from ) {
97117 included_resources = Converter . json_array2resources_array_by_type ( document_from . included , false ) ;
98118 }
99119
100120 if ( angular . isArray ( document_from . data ) ) {
101- Converter . _buildResources ( document_from , < Jsonapi . ICollection > resource_dest , schema , included_resources ) ;
121+ Converter . _buildCollection ( document_from , < Jsonapi . ICollection > resource_dest , schema , included_resources ) ;
102122 } else {
103123 Converter . _buildResource ( document_from . data , < Jsonapi . IResource > resource_dest , schema , included_resources ) ;
104124 }
105125 }
106126
107- static _buildResources (
108- document_from : Jsonapi . ICollection ,
109- resource_dest : Jsonapi . ICollection ,
127+ static _buildCollection (
128+ collection_data_from : IDataCollection ,
129+ collection_dest : Jsonapi . ICollection ,
110130 schema : Jsonapi . ISchema ,
111- included_resources : Object
131+ included_resources : Jsonapi . IResource [ ]
112132 ) {
113133 // sometime get Cannot set property 'number' of undefined (page)
114- if ( resource_dest . page && document_from [ 'meta' ] ) {
115- resource_dest . page . number = document_from [ 'meta' ] [ 'page' ] ? document_from [ 'meta' ] [ 'page' ] : 1 ;
116- resource_dest . page .
117- resources_per_page = document_from [ 'meta' ] [ 'resources_per_page' ] ? document_from [ 'meta' ] [ 'resources_per_page' ] : null ;
118- resource_dest . page . total_resources = document_from [ 'meta' ] [ 'total_resources' ] ? document_from [ 'meta' ] [ 'total_resources' ] : null ;
134+ if ( collection_dest . page && collection_data_from [ 'meta' ] ) {
135+ collection_dest . page . number = collection_data_from [ 'meta' ] [ 'page' ] || 1 ;
136+ collection_dest . page . resources_per_page = collection_data_from [ 'meta' ] [ 'resources_per_page' ] || null ;
137+ collection_dest . page . total_resources = collection_data_from [ 'meta' ] [ 'total_resources' ] || null ;
119138 }
120139
121140 // convert and add new dataresoures to final collection
122- let dataresource : IDataResource ;
123141 let new_ids = { } ;
124- for ( dataresource of document_from . data ) {
142+ for ( let dataresource of collection_data_from . data ) {
125143 let service = Converter . getService ( dataresource . type ) ;
126- if ( ! ( dataresource . id in resource_dest ) ) {
127- resource_dest [ dataresource . id ] = new ( < any > service . constructor ) ( ) ;
128- resource_dest [ dataresource . id ] . reset ( ) ;
144+ if ( ! ( dataresource . id in collection_dest ) ) {
145+ collection_dest [ dataresource . id ] = new ( < any > service . constructor ) ( ) ;
146+ collection_dest [ dataresource . id ] . reset ( ) ;
129147 }
130- Converter . _buildResource ( dataresource , resource_dest [ dataresource . id ] , schema , included_resources ) ;
148+ Converter . _buildResource ( dataresource , collection_dest [ dataresource . id ] , schema , included_resources ) ;
131149 new_ids [ dataresource . id ] = dataresource . id ;
132150 }
133151
134152 /*
135153 remove old members of collection (bug, for example, when request something like orders/10/details and has new ids)
136154 */
137- angular . forEach ( resource_dest , resource => {
155+ angular . forEach ( collection_dest , resource => {
138156 if ( ! ( resource . id in new_ids ) ) {
139- delete resource_dest [ resource . id ] ;
157+ delete collection_dest [ resource . id ] ;
140158 }
141159 } ) ;
142160 }
143161
144162 static _buildResource (
145- document_from : IDataResource ,
163+ resource_data_from : IDataResource ,
146164 resource_dest : Jsonapi . IResource ,
147165 schema : Jsonapi . ISchema ,
148- included_resources : Object
166+ included_resources : Jsonapi . IResource [ ]
149167 ) {
150- resource_dest . attributes = document_from . attributes ;
151- resource_dest . id = document_from . id ;
168+ resource_dest . attributes = resource_data_from . attributes ;
169+ resource_dest . id = resource_data_from . id ;
152170 resource_dest . is_new = false ;
153171
154172 let relationships_converter = new ResourceRelationshipsConverter (
155173 Converter . getService ,
156- document_from . relationships ,
174+ resource_data_from . relationships ,
157175 resource_dest . relationships ,
158176 included_resources ,
159177 schema
0 commit comments