@@ -4,6 +4,7 @@ import * as models from '../../../models/models';
44import { Http , HTTP_BINDINGS , Headers , Response } from 'angular2/http' ;
55import { AlbumDeletePrompt } from '../album-delete-prompt/album-delete-prompt' ;
66import { FormField } from '../form-field/form-field' ;
7+ import * as AspNet from './AspNetUtil' ;
78
89@ng . Component ( {
910 selector : 'album-edit'
@@ -67,21 +68,11 @@ export class AlbumEdit {
6768 var controls = this . form . controls ;
6869 var albumId = this . originalAlbum . AlbumId ;
6970
70- this . _putJson ( `/api/albums/${ albumId } /update` , this . form . value ) . then ( response => {
71+ this . _putJson ( `/api/albums/${ albumId } /update` , this . form . value ) . subscribe ( response => {
7172 if ( response . status === 200 ) {
7273 this . changesSaved = true ;
7374 } else {
74- var errors = < ValidationResponse > ( response . json ( ) ) ;
75- Object . keys ( errors ) . forEach ( key => {
76- errors [ key ] . forEach ( errorMessage => {
77- // TODO: There has to be a better API for this
78- if ( ! this . form . controls [ key ] . errors ) {
79- ( < any > this . form . controls [ key ] ) . _errors = { } ;
80- }
81-
82- this . form . controls [ key ] . errors [ errorMessage ] = true ;
83- } ) ;
84- } ) ;
75+ AspNet . Validation . showValidationErrors ( response , this . form ) ;
8576 }
8677 } ) ;
8778 }
@@ -91,15 +82,15 @@ export class AlbumEdit {
9182 return / ^ \d + \. \d + $ / . test ( control . value ) ? null : { Price : true } ;
9283 }
9384
94- private _putJson ( url : string , body : any ) : Promise < Response > {
95- return new Promise ( ( resolve , reject ) => {
96- this . _http . put ( url , JSON . stringify ( body ) , {
97- headers : new Headers ( { 'Content-Type' : 'application/json' } )
98- } ) . subscribe ( resolve ) ;
85+ // Need feedback on whether this really is the easiest way to PUT some JSON
86+ private _putJson ( url : string , body : any ) : Subscribable < Response > {
87+ return this . _http . put ( url , JSON . stringify ( body ) , {
88+ headers : new Headers ( { 'Content-Type' : 'application/json' } )
9989 } ) ;
10090 }
10191}
10292
103- interface ValidationResponse {
104- [ propertyName : string ] : string [ ] ;
93+ // TODO: Figure out what type declaration is provided by Angular/RxJs and use that instead
94+ interface Subscribable < T > {
95+ subscribe ( callback : ( response : Response ) => void ) : void ;
10596}
0 commit comments