Transform controller function to class.

This commit is contained in:
Alexis Lahouze 2017-07-21 23:06:59 +02:00
parent a398752c0c
commit 8626c78708
2 changed files with 38 additions and 30 deletions

View File

@ -24,7 +24,7 @@ var ngMessages = require('angular-messages'),
ngUiNotification = require('angular-ui-notification'), ngUiNotification = require('angular-ui-notification'),
ngStrap = require('angular-strap'); ngStrap = require('angular-strap');
var ScheduleController = require('./schedule.controller'); import { ScheduleController } from './schedule.controller';
import { ScheduleFactory } from './schedule.factory'; import { ScheduleFactory } from './schedule.factory';
export default angular.module('accountant.scheduler', [ export default angular.module('accountant.scheduler', [

View File

@ -1,48 +1,59 @@
var scheduleFormTmpl = require('./schedule.form.tmpl.html'), var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
module.exports= function($rootScope, $stateParams, Notification, ScheduledOperation, $log, $modal) { export class ScheduleController{
var vm = this; $inject=['$rootScope', '$stateParams', 'Notification', 'ScheduledOperation', '$log', '$modal']
// Operation store. operations = [];
vm.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. * Add a new operation at the beginning of th array.
*/ */
vm.add = function() { add = function() {
var operation = new ScheduledOperation({ var operation = new this.ScheduledOperation({
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
account_id: $stateParams.accountId account_id: this.$stateParams.accountId
}); });
return vm.modify(operation); return this.modify(operation);
}; };
/* /*
* Load operations. * Load operations.
*/ */
vm.load = function() { load = function() {
return ScheduledOperation.query({ return this.ScheduledOperation.query({
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
account_id: $stateParams.accountId account_id: this.$stateParams.accountId
}); });
}; };
/* /*
* Save operation. * Save operation.
*/ */
vm.save = function(operation) { save = function(operation) {
return operation.$save().then(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; return operation;
}, function(result){ }, 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 '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. * Delete an operation and return a promise.
*/ */
vm.confirmDelete = function(operation) { confirmDelete = function(operation) {
var title = "Delete operation #" + operation.id; var title = "Delete operation #" + operation.id;
$modal({ this.$modal({
templateUrl: scheduleDeleteTmpl, templateUrl: scheduleDeleteTmpl,
controller: function($scope, title, operation, $delete) { controller: function($scope, title, operation, $delete) {
$scope.title = title; $scope.title = title;
@ -67,7 +78,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat
locals: { locals: {
title: title, title: title,
operation: operation, operation: operation,
$delete: vm.delete $delete: this.delete
} }
}); });
}; };
@ -75,17 +86,17 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat
/* /*
* Delete operation. * Delete operation.
*/ */
vm.delete = function(operation) { delete = function(operation) {
var id = operation.id; var id = operation.id;
return operation.$delete().then(function() { 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; return operation;
}, function(result) { }, function(result) {
Notification.error( this.Notification.error(
'An error occurred while trying to delete scheduled operation #' + 'An error occurred while trying to delete scheduled operation #' +
id + ':<br />' + result id + ':<br />' + result
); );
@ -96,7 +107,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat
* Open the popup to modify the operation, save it on confirm. * Open the popup to modify the operation, save it on confirm.
* @returns a promise. * @returns a promise.
*/ */
vm.modify = function(operation) { modify = function(operation) {
// FIXME Alexis Lahouze 2017-06-15 i18n // FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Operation"; var title = "Operation";
@ -104,7 +115,7 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat
title = title + " #" + operation.id; title = title + " #" + operation.id;
} }
$modal({ this.$modal({
templateUrl: scheduleFormTmpl, templateUrl: scheduleFormTmpl,
controller: function($scope, title, operation, $save) { controller: function($scope, title, operation, $save) {
$scope.title = title; $scope.title = title;
@ -117,11 +128,8 @@ module.exports= function($rootScope, $stateParams, Notification, ScheduledOperat
locals: { locals: {
title: title, title: title,
operation: operation, operation: operation,
$save: vm.save $save: this.save
} }
}); });
}; };
// Load operations on controller initialization.
vm.operations = vm.load();
}; };