@@ -163,6 +163,11 @@ export class Resource implements IResource {
163163 return this . __exec ( null , params , fc_success , fc_error , 'all' ) ;
164164 }
165165
166+ // just for debuggin purposes
167+ public getCachedResources ( ) {
168+ return this . getService ( ) . memorycache . resources ;
169+ }
170+
166171 public save < T extends IResource > ( params ?: Object | Function , fc_success ?: Function , fc_error ?: Function ) : Array < T > {
167172 return this . __exec ( null , params , fc_success , fc_error , 'save' ) ;
168173 }
@@ -406,10 +411,32 @@ export class Resource implements IResource {
406411 this . getService ( ) . memorycache . clearAllCollections ( ) ;
407412 }
408413
409- this . id = success . data . data . id ;
410-
411- Converter . build ( success . data , this , this . schema ) ;
412- this . getService ( ) . memorycache . setResource ( this ) ;
414+ // is a resource?
415+ if ( 'id' in success . data . data ) {
416+ this . id = success . data . data . id ;
417+ Converter . build ( success . data , this , this . schema ) ;
418+ /*
419+ Si lo guardo en la caché, luego no queda bindeado con la vista
420+ Usar {{ $ctrl.service.getCachedResources() | json }}, agregar uno nuevo, editar
421+ */
422+ // this.getService().memorycache.setResource(this);
423+ } else if ( angular . isArray ( success . data . data ) ) {
424+ console . warn ( 'Server return a collection when we save()' , success . data . data ) ;
425+
426+ /*
427+ we request the service again, because server maybe are giving
428+ us another type of resource (getService(resource.type))
429+ */
430+ let tempororay_collection = this . getService ( ) . memorycache . getCollection ( 'justAnUpdate' ) ;
431+ Converter . build ( success . data , tempororay_collection , this . schema ) ;
432+ angular . forEach ( tempororay_collection , ( resource_value : IResource , key : string ) => {
433+ let res = Converter . getService ( resource_value . type ) . memorycache . resources [ resource_value . id ] ;
434+ Converter . getService ( resource_value . type ) . memorycache . setResource ( resource_value ) ;
435+ res . id = res . id + 'x' ;
436+ } ) ;
437+
438+ console . warn ( 'Temporal collection for a resource_value update' , tempororay_collection ) ;
439+ }
413440
414441 this . runFc ( fc_success , success ) ;
415442 } ,
@@ -434,18 +461,14 @@ export class Resource implements IResource {
434461 this . relationships [ type_alias ] = { data : { } } ;
435462 }
436463
437- if ( this . schema . relationships [ type_alias ] . hasMany ) {
464+ if ( type_alias in this . schema . relationships && this . schema . relationships [ type_alias ] . hasMany ) {
438465 this . relationships [ type_alias ] [ 'data' ] [ object_key ] = resource ;
439466 } else {
440467 this . relationships [ type_alias ] [ 'data' ] = resource ;
441468 }
442469 }
443470
444471 public addRelationships ( resources : ICollection , type_alias : string ) {
445- if ( ! this . schema . relationships [ type_alias ] . hasMany ) {
446- console . warn ( 'addRelationships not supported on ' + this . type + ' schema.' ) ;
447- }
448-
449472 if ( ! ( type_alias in this . relationships ) ) {
450473 this . relationships [ type_alias ] = { data : { } } ;
451474 } else {
0 commit comments