Skip to content

Commit

Permalink
feat(request): request content component and viewchildren visibility …
Browse files Browse the repository at this point in the history
…control
  • Loading branch information
michael-xd committed Feb 7, 2017
1 parent 5100547 commit 45f49b0
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/components/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContentChild, Directive, EventEmitter, Input, Output } from '@angular/core';
import { Http } from '@mbamobi/http';
import { StateEmpty, StateError, StateLoading } from './states';
import { StateEmpty, StateError, StateLoading, StateContent } from './states';

@Directive({
selector: '[request]'
Expand All @@ -19,6 +19,8 @@ export class Request {

@ContentChild(StateError) error: any;

@ContentChild(StateContent) content: any;

constructor(
private http: Http
) {}
Expand Down Expand Up @@ -47,13 +49,20 @@ export class Request {
}

request() {
if (this.loading) {
this.loading.present();
}

this.dismissError();
this.dismissNoRecords();
this.dismissContent();

this.http.request(
this.url,
this.params,
this.requestOptions,
this.options
).subscribe((result: any) => {

this.dismissLoading();

if (this.noRecords) {
Expand All @@ -62,10 +71,11 @@ export class Request {

if (!isPresent) {
this.loaded.emit(result);
this.presentContent();
}

} else {
this.loaded.emit(result);
this.presentContent();
}
}, (error) => {
this.dismissLoading();
Expand All @@ -86,4 +96,27 @@ export class Request {
this.loading.dismiss();
}
}

private dismissError() {
if (this.error) {
this.error.dismiss();
}
}

private dismissNoRecords() {
if (this.noRecords) {
this.noRecords.dismiss();
}
}

private dismissContent() {
if (this.content) {
this.content.dismiss();
}
}
private presentContent() {
if (this.content) {
this.content.present();
}
}
}

0 comments on commit 45f49b0

Please sign in to comment.