Skip to content

Commit

Permalink
feat(components): refactor state empty support jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonornela committed Jan 3, 2017
1 parent 1022ca8 commit 2efc850
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@angular/platform-browser": "2.2.1",
"@angular/compiler": "2.2.1",
"@angular/compiler-cli": "2.2.1",
"@ramonornela/jsonpath": "0.2.10",
"@ramonornela/url-resolver": "~0.0.9",
"@ramonornela/configuration": "~0.0.3",
"@ramonornela/http": "nightly",
Expand Down
12 changes: 9 additions & 3 deletions src/components/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ export class Request {

this.dismissLoading();

this.loaded.emit(result);

if (this.noRecords) {
this.noRecords.present();
this.noRecords.response = result;
let isPresent = this.noRecords.present();

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

} else {
this.loaded.emit(result);
}
}, (error) => {
this.dismissLoading();
Expand Down
55 changes: 45 additions & 10 deletions src/components/states.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Input, Optional } from '@angular/core';
import { ViewController } from 'ionic-angular';
import { Response } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import jp from '@ramonornela/jsonpath';

@Component({
selector: 'loading',
Expand Down Expand Up @@ -42,24 +43,58 @@ export class StateLoading {
export class StateEmpty {
enabled: boolean = false;

@Input() variableBind: any;
@Input() map: any;

constructor(
@Optional() private ctrl: ViewController
) {}
@Input() response: any;

present() {
this.enabled = false;
let dataBind = this.ctrl._cmp.instance[this.variableBind];
present(): boolean {
let enabled = this.isSimpleResult(this.response);
if (enabled) {
this.enabled = enabled;
return true;
}

if (!(this.response instanceof Response)) {
throw new Error('Data type response invalid!');
}

let json = this.response.json();

if (!json) {
throw new Error('Json is required');
}

if (this.map) {
let data = jp.query(json, this.map);

if (this.ctrl && this.ctrl._cmp && this.variableBind && !dataBind) {
this.enabled = true;
if (data.length === 0) {
this.enabled = true;
return true;
}

return false;
}

enabled = this.isSimpleResult(json);
if (enabled) {
this.enabled = enabled;
return true;
}

return false;
}

dismiss() {
this.enabled = false;
}

private isSimpleResult(data: any) {
if ((Array.isArray(data) && data.length === 0) || (typeof data === 'object' && Object.keys(data).length === 0)) {
return true;
}

return false;
}
}

@Component({
Expand Down

0 comments on commit 2efc850

Please sign in to comment.