Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 1 | import {IXosTableCfg} from '../../core/table/table'; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 2 | import {IStoreService} from '../../datasources/stores/slices.store'; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 3 | export interface IXosCrudData { |
| 4 | title: string; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 5 | store: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 6 | xosTableCfg: IXosTableCfg; |
| 7 | } |
| 8 | |
| 9 | class CrudController { |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 10 | // TODO dynamically inject store |
| 11 | static $inject = ['$state', '$injector', '$scope']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 12 | |
| 13 | public data: IXosCrudData; |
| 14 | public tableCfg: IXosTableCfg; |
| 15 | public title: string; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 16 | public storeName: string; |
| 17 | public store: IStoreService; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 18 | public tableData: any[]; |
| 19 | |
| 20 | constructor( |
| 21 | private $state: angular.ui.IStateService, |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 22 | private $injector: angular.Injectable<any>, |
| 23 | private $scope: angular.IScope |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 24 | ) { |
| 25 | this.data = this.$state.current.data; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 26 | this.tableCfg = this.data.xosTableCfg; |
| 27 | this.title = this.data.title; |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | this.storeName = this.data.store; |
| 29 | this.store = this.$injector.get(this.storeName); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 31 | 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 Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | export const xosCrud: angular.IComponentOptions = { |
| 44 | template: require('./crud.html'), |
| 45 | controllerAs: 'vm', |
| 46 | controller: CrudController |
| 47 | }; |