#440 basic REST services support for dropdown widget

This commit is contained in:
Denys Vuika
2016-07-25 17:06:05 +01:00
parent 978b9baf6a
commit 9d34ea1d7f
2 changed files with 85 additions and 35 deletions

View File

@@ -15,7 +15,10 @@
* limitations under the License.
*/
import { Component } from '@angular/core';
import { Component, OnInit, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { Http } from '@angular/http';
import { ObjectUtils } from 'ng2-alfresco-core';
import { WidgetComponent } from './../widget.component';
declare let __moduleName: string;
@@ -27,6 +30,53 @@ declare var componentHandler;
templateUrl: './dropdown.widget.html',
styleUrls: ['./dropdown.widget.css']
})
export class DropdownWidget extends WidgetComponent {
export class DropdownWidget extends WidgetComponent implements OnInit {
constructor(private http: Http,
private zone: NgZone) {
super();
}
ngOnInit() {
if (this.field &&
this.field.optionType === 'rest' &&
this.field.restUrl &&
this.field.restIdProperty &&
this.field.restLabelProperty) {
let url = `${this.field.restUrl}`;
this.http.get(url).subscribe(
response => {
let json: any = response.json();
this.loadFromJson(json);
},
this.handleError
);
}
}
// TODO: support 'restResponsePath'
private loadFromJson(json: any) {
if (json instanceof Array) {
let options = json.map(obj => {
return {
id: ObjectUtils.getValue(obj, this.field.restIdProperty).toString(),
name: ObjectUtils.getValue(obj, this.field.restLabelProperty).toString()
};
});
this.field.options = options;
this.field.updateForm();
}
}
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}