AngularJS (CRUD restoration controllers, Simplify / DRY?)

Below is a small example of what I want to simplify. I have the same structure created for several other models, and I was wondering if I could interfere with printing the same functionality, but using only different messages / models.

Is it possible to reuse the controller and pass parameters? (In this case, the model name + messages that should be displayed ...). Ideally, I just want the main CRUD controller to be reused, but just in case allow custom methods.

angular.module('employees.controllers', ["templates.app", "ui.bootstrap"]) .controller("EmployeeListController", ["$scope", "$modal", "Restangular", function ($scope, $modal, Restangular) { var Employee = Restangular.all("employees"); Employee.getList().then(function (employees) { $scope.employees = employees; }) $scope.createEmployee = function () { $modal.open({ templateUrl: 'employees/partials/employees.manage.modal.tpl.html', controller: 'EmployeeCreateController' }).result.then(function (employee) { Employee.post(employee).then(function (newEmployee) { $scope.messageService.addMessage("success", "Employee was successfully created!"); $scope.employees.push(newEmployee); }); }); }; $scope.deleteEmployee = function (employee) { employee.remove().then(function () { $scope.messageService.addMessage("success", "Employee was successfully deleted!"); $scope.employees = _.without($scope.employees, employee); }); }; $scope.editEmployee = function (originalEmployee) { $modal.open({ templateUrl: 'employees/partials/employees.manage.modal.tpl.html', controller: 'EmployeeUpdateController', resolve: { employee: function () { return Restangular.copy(originalEmployee); } } }).result.then(function (employee) { employee.put().then(function (updated_employee) { $scope.messageService.addMessage("success", "Employee was successfully updated!"); var originalIndex = _.indexOf($scope.employees, originalEmployee); $scope.employees[originalIndex] = updated_employee; }); }); }; }]).controller("EmployeeCreateController", ["$scope", "$modalInstance", "$timeout", function ($scope, $modalInstance, $timeout) { $scope.createMode = true; $scope.form = {}; $scope.employee = {}; $scope.datepicker = {}; $scope.ok = function () { if ($scope.form.createResource.$valid) { $modalInstance.close($scope.employee); } }; $scope.open = function () { $timeout(function () { $scope.datepicker.opened = true; }); }; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; }]).controller("EmployeeUpdateController", ["$scope", "$modalInstance", "employee", function ($scope, $modalInstance, employee) { $scope.createMode = false; $scope.form = {}; $scope.employee = employee; $scope.ok = function () { if ($scope.form.createResource.$valid) { $modalInstance.close($scope.employee); } }; $scope.cancel = function () { $modalInstance.dismiss("cancel"); }; }]); 
+1
angularjs restangular
source share

No one has answered this question yet.

See similar questions:

0
Remote deletion from list after deletion () does not work

or similar:

4523
"Thinking in AngularJS" if I have jQuery background?
3178
AngularJS: Service vs provider vs factory
1690
How does data binding work in AngularJS?
1195
How do I access the $ scope variable in the browser console using AngularJS?
1073
How to use $ scope. $ Watch and $ scope. $ Apply in AngularJS?
998
What is the difference between "@" and "=" in scope in AngularJS?
912
'this' vs $ scope in AngularJS controllers
496
Fighting AngularJS controller twice
one
Angularjs modal updates my parent scope before results are sent back
0
Pushing objects into an array without working with modal corner js

All Articles