AngularJS(Restangular CRUD,Simplify / DRY Controllers?)

时间:2014-02-28 12:22:37

标签: angularjs restangular

下面是我想要简化的一个小例子。我为其他几个模型设置了相同的结构,并且想知道我是否可以阻止键入相同的功能但只使用不同的消息/模型。

是否可以重复使用控制器和放大器。传递参数? (在这种情况下,模型的名称+需要显示的消息......)。理想情况下,我只是希望重用基本的CRUD控制器,但允许使用自定义方法,以防万一。

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");
    };
}]);

0 个答案:

没有答案