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"); }; }]);
angularjs restangular
Busata
source share