Skip to content

Commit ea4c668

Browse files
Make the edit form and delete dialog work again
1 parent 381b7b8 commit ea4c668

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

samples/angular/MusicStore/wwwroot/ng-app/components/admin/album-delete-prompt/album-delete-prompt.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ import * as models from '../../../models/models';
1010
directives: [NgIf]
1111
})
1212
export class AlbumDeletePrompt {
13-
private modalElement: any;
1413
public album: models.Album;
1514

16-
constructor(@ng.Inject(ng.ElementRef) elementRef: ng.ElementRef) {
17-
if (typeof window !== 'undefined') {
18-
this.modalElement = (<any>window).jQuery(".modal", elementRef.nativeElement);
19-
}
15+
constructor(@ng.Inject(ng.ElementRef) private _elementRef: ng.ElementRef) {
2016
}
2117

2218
public show(album: models.Album) {
2319
this.album = album;
24-
this.modalElement.modal();
20+
21+
// Consider rewriting this using Angular 2's "Renderer" API so as to avoid direct DOM access
22+
(<any>window).jQuery(".modal", this._elementRef.nativeElement).modal();
2523
}
2624
}

samples/angular/MusicStore/wwwroot/ng-app/components/admin/album-edit/album-edit.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
<h2>Album <small>Edit</small></h2>
22
<hr />
33

4-
<form class="form-horizontal" [ngFormModel]="form" (ng-submit)="onSubmitModelBased()">
4+
<form class="form-horizontal" [ngFormModel]="form" (ngSubmit)="onSubmitModelBased()">
55
<form-field label="Artist" [validate]="form.controls.ArtistId">
6-
<select class="form-control" ng-control="ArtistId">
6+
<select class="form-control" ngControl="ArtistId">
77
<option value="0">-- choose Artist --</option>
88
<option *ngFor="#artist of artists" [value]="artist.ArtistId">{{ artist.Name }}</option>
99
</select>
1010
</form-field>
1111

1212
<form-field label="Genre" [validate]="form.controls.GenreId">
13-
<select class="form-control" ng-control="GenreId">
13+
<select class="form-control" ngControl="GenreId">
1414
<option value="0">-- choose Genre --</option>
1515
<option *ngFor="#genre of genres" [value]="genre.GenreId">{{ genre.Name }}</option>
1616
</select>
1717
</form-field>
1818

1919
<form-field label="Title" [validate]="form.controls.Title">
20-
<input class="form-control" type="text" ng-control="Title">
20+
<input class="form-control" type="text" ngControl="Title">
2121
</form-field>
2222

2323
<form-field label="Price" [validate]="form.controls.Price">
2424
<div class="input-group">
2525
<span class="input-group-addon">$</span>
26-
<input class="form-control" type="text" ng-control="Price">
26+
<input class="form-control" type="text" ngControl="Price">
2727
</div>
2828
</form-field>
2929

3030
<form-field label="Album Art URL" [validate]="form.controls.AlbumArtUrl">
31-
<input class="form-control" ng-control="AlbumArtUrl">
31+
<input class="form-control" ngControl="AlbumArtUrl">
3232
</form-field>
3333

3434
<form-field label="Album Art">

samples/angular/MusicStore/wwwroot/ng-app/components/admin/album-edit/album-edit.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as ng from 'angular2/core';
2+
import { Observable } from 'rxjs';
23
import { Control, ControlGroup, FormBuilder, Validators, NgIf, NgFor, FORM_DIRECTIVES } from 'angular2/common';
34
import * as router from 'angular2/router';
45
import * as models from '../../../models/models';
@@ -20,6 +21,7 @@ export class AlbumEdit {
2021
public genres: models.Genre[];
2122
public originalAlbum: models.Album;
2223
public changesSaved: boolean;
24+
public formErrors: string[] = [];
2325

2426
private _http: Http;
2527

@@ -69,12 +71,10 @@ export class AlbumEdit {
6971
var controls = this.form.controls;
7072
var albumId = this.originalAlbum.AlbumId;
7173

72-
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).subscribe(response => {
73-
if (response.status === 200) {
74-
this.changesSaved = true;
75-
} else {
76-
AspNet.Validation.showValidationErrors(response, this.form);
77-
}
74+
this._putJson(`/api/albums/${ albumId }/update`, this.form.value).subscribe(successResponse => {
75+
this.changesSaved = true;
76+
}, errorResponse => {
77+
AspNet.Validation.showValidationErrors(errorResponse, this.form);
7878
});
7979
}
8080
}
@@ -84,18 +84,13 @@ export class AlbumEdit {
8484
}
8585

8686
// Need feedback on whether this really is the easiest way to PUT some JSON
87-
private _putJson(url: string, body: any): Subscribable<Response> {
87+
private _putJson(url: string, body: any): Observable<Response> {
8888
return this._http.put(url, JSON.stringify(body), {
8989
headers: new Headers({ 'Content-Type': 'application/json' })
9090
});
9191
}
92-
93-
public get formErrors(): string[] {
94-
return this.form.dirty ? Object.keys(this.form.errors || {}) : [];
95-
}
96-
}
9792

98-
// TODO: Figure out what type declaration is provided by Angular/RxJs and use that instead
99-
interface Subscribable<T> {
100-
subscribe(callback: (response: Response) => void): void;
101-
}
93+
private ngDoCheck() {
94+
this.formErrors = this.form.dirty ? Object.keys(this.form.errors || {}) : [];
95+
}
96+
}

samples/angular/MusicStore/wwwroot/ng-app/components/admin/form-field/form-field.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { NgIf, NgFor, AbstractControl } from 'angular2/common';
1010
directives: [NgIf, NgFor]
1111
})
1212
export class FormField {
13+
public errorMessages: string[] = [];
1314
private validate: AbstractControl;
14-
15-
public get errorMessages() {
15+
16+
private ngDoCheck() {
1617
var errors = (this.validate && this.validate.dirty && this.validate.errors) || {};
17-
return Object.keys(errors).map(key => {
18+
this.errorMessages = Object.keys(errors).map(key => {
1819
return 'Error: ' + key;
1920
});
2021
}

0 commit comments

Comments
 (0)