blob: 853669c545b3790cbaea66277d686a774a45d7a4 [file] [log] [blame]
Matteo Scandolo9f87f302016-12-13 18:11:10 -08001import {IXosTableCfg} from '../../core/table/table';
Matteo Scandolo035c5932016-12-14 09:55:15 -08002import {IStoreService} from '../../datasources/stores/slices.store';
Matteo Scandolo9f87f302016-12-13 18:11:10 -08003export interface IXosCrudData {
4 title: string;
Matteo Scandolo035c5932016-12-14 09:55:15 -08005 store: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -08006 xosTableCfg: IXosTableCfg;
7}
8
9class CrudController {
Matteo Scandolo035c5932016-12-14 09:55:15 -080010 // TODO dynamically inject store
11 static $inject = ['$state', '$injector', '$scope'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080012
13 public data: IXosCrudData;
14 public tableCfg: IXosTableCfg;
15 public title: string;
Matteo Scandolo035c5932016-12-14 09:55:15 -080016 public storeName: string;
17 public store: IStoreService;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080018 public tableData: any[];
19
20 constructor(
21 private $state: angular.ui.IStateService,
Matteo Scandolo035c5932016-12-14 09:55:15 -080022 private $injector: angular.Injectable<any>,
23 private $scope: angular.IScope
Matteo Scandolo9f87f302016-12-13 18:11:10 -080024 ) {
25 this.data = this.$state.current.data;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080026 this.tableCfg = this.data.xosTableCfg;
27 this.title = this.data.title;
Matteo Scandolo035c5932016-12-14 09:55:15 -080028 this.storeName = this.data.store;
29 this.store = this.$injector.get(this.storeName);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080030
Matteo Scandolo035c5932016-12-14 09:55:15 -080031 this.store.query()
32 .subscribe(
33 (event) => {
34 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
35 $scope.$evalAsync(() => {
36 this.tableData = event;
37 });
38 }
39 );
Matteo Scandolo9f87f302016-12-13 18:11:10 -080040 }
41}
42
43export const xosCrud: angular.IComponentOptions = {
44 template: require('./crud.html'),
45 controllerAs: 'vm',
46 controller: CrudController
47};