From 8626c7870854f632c0f74c78a54c25ba16847b11 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Fri, 21 Jul 2017 23:06:59 +0200 Subject: [PATCH] Transform controller function to class. --- src/scheduler/index.ts | 2 +- src/scheduler/schedule.controller.ts | 66 ++++++++++++++++------------ 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/scheduler/index.ts b/src/scheduler/index.ts index a351aef..f70731d 100644 --- a/src/scheduler/index.ts +++ b/src/scheduler/index.ts @@ -24,7 +24,7 @@ var ngMessages = require('angular-messages'), ngUiNotification = require('angular-ui-notification'), ngStrap = require('angular-strap'); -var ScheduleController = require('./schedule.controller'); +import { ScheduleController } from './schedule.controller'; import { ScheduleFactory } from './schedule.factory'; export default angular.module('accountant.scheduler', [ diff --git a/src/scheduler/schedule.controller.ts b/src/scheduler/schedule.controller.ts index bb064dc..180b2c0 100644 --- a/src/scheduler/schedule.controller.ts +++ b/src/scheduler/schedule.controller.ts @@ -1,48 +1,59 @@ var scheduleFormTmpl = require('./schedule.form.tmpl.html'), scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); -module.exports= function($rootScope, $stateParams, Notification, ScheduledOperation, $log, $modal) { - var vm = this; +export class ScheduleController{ + $inject=['$rootScope', '$stateParams', 'Notification', 'ScheduledOperation', '$log', '$modal'] - // Operation store. - vm.operations = []; + operations = []; + + constructor( + private $rootScope, + private $stateParams, + private Notification, + private ScheduledOperation, + private $log, + private $modal + ) { + // Load operations on controller initialization. + this.operations = this.load(); + } /* * Add a new operation at the beginning of th array. */ - vm.add = function() { - var operation = new ScheduledOperation({ + add = function() { + var operation = new this.ScheduledOperation({ // eslint-disable-next-line camelcase - account_id: $stateParams.accountId + account_id: this.$stateParams.accountId }); - return vm.modify(operation); + return this.modify(operation); }; /* * Load operations. */ - vm.load = function() { - return ScheduledOperation.query({ + load = function() { + return this.ScheduledOperation.query({ // eslint-disable-next-line camelcase - account_id: $stateParams.accountId + account_id: this.$stateParams.accountId }); }; /* * Save operation. */ - vm.save = function(operation) { + save = function(operation) { return operation.$save().then(function(operation) { - Notification.success('Scheduled operation #' + operation.id + ' saved.'); + this.Notification.success('Scheduled operation #' + operation.id + ' saved.'); - vm.operations = vm.load(); + this.operations = this.load(); return operation; }, function(result){ - $log.error('Error while saving scheduled operation', operation, result); + this.$log.error('Error while saving scheduled operation', operation, result); - Notification.error( + this.Notification.error( 'Error while saving scheduled operation: ' + result.message ); }); @@ -51,10 +62,10 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat /* * Delete an operation and return a promise. */ - vm.confirmDelete = function(operation) { + confirmDelete = function(operation) { var title = "Delete operation #" + operation.id; - $modal({ + this.$modal({ templateUrl: scheduleDeleteTmpl, controller: function($scope, title, operation, $delete) { $scope.title = title; @@ -67,7 +78,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat locals: { title: title, operation: operation, - $delete: vm.delete + $delete: this.delete } }); }; @@ -75,17 +86,17 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat /* * Delete operation. */ - vm.delete = function(operation) { + delete = function(operation) { var id = operation.id; return operation.$delete().then(function() { - Notification.success('Scheduled operation #' + id + ' deleted.'); + this.Notification.success('Scheduled operation #' + id + ' deleted.'); - vm.operations = vm.load(); + this.operations = this.load(); return operation; }, function(result) { - Notification.error( + this.Notification.error( 'An error occurred while trying to delete scheduled operation #' + id + ':
' + result ); @@ -96,7 +107,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ - vm.modify = function(operation) { + modify = function(operation) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Operation"; @@ -104,7 +115,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat title = title + " #" + operation.id; } - $modal({ + this.$modal({ templateUrl: scheduleFormTmpl, controller: function($scope, title, operation, $save) { $scope.title = title; @@ -117,11 +128,8 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat locals: { title: title, operation: operation, - $save: vm.save + $save: this.save } }); }; - - // Load operations on controller initialization. - vm.operations = vm.load(); };