Mam następujący kod:

        modalOpen: function ($scope) {
            $scope.view = $scope.modal.action;
            $scope.modalForm.$setPristine();
            $scope.view = $scope.modal.action;
            if ($scope.modal.action != "delete") {
                $scope.modal.timeout = window.setTimeout(function () {
                    $scope.modal.interval = window.setInterval(function () {
                        if (($scope.view == "edit" || $scope.view == "new") && !$scope.modalForm.$pristine) {
                            factory.modalSubmit($scope, $scope.modal.data, true);
                        }
                    }, 5 * 60 * 1000);
                    if (($scope.view == "edit" || $scope.view == "new") && !$scope.modalForm.$pristine) {
                        factory.modalSubmit($scope, $scope.modal.data, true);
                    }
                }, 5 * 60 * 1000);
            }
        },

Czy istnieje sposób, w jaki mogłem kapsułkować:

if (($scope.view == "edit" || $scope.view == "new") && !$scope.modalForm.$pristine) {
                            factory.modalSubmit($scope, $scope.modal.data, true);
                        }

Więc mogę go ponownie wykorzystać i nie musi powtórzyć te same linie?

-4
Samantha J T Star 6 grudzień 2013, 12:14

2 odpowiedzi

Najlepsza odpowiedź
    modalOpen: function ($scope) {
        $scope.view = $scope.modal.action;
        $scope.modalForm.$setPristine();
        $scope.view = $scope.modal.action;

        function maybe_submit() {
            if (($scope.view == "edit" || $scope.view == "new") && !$scope.modalForm.$pristine) {
                factory.modalSubmit($scope, $scope.modal.data, true);
            }
        }

        if ($scope.modal.action != "delete") {
            $scope.modal.timeout = window.setTimeout(function () {
                $scope.modal.interval = window.setInterval(maybe_submit, 5 * 60 * 1000);
                maybe_submit();
            }, 5 * 60 * 1000);
        }
    },
1
Barmar 6 grudzień 2013, 08:19
var modalSubmit = function () {
     if (($scope.view == "edit" || $scope.view == "new") && !$scope.modalForm.$pristine) {
        factory.modalSubmit($scope, $scope.modal.data, true);
     }
}
$scope.modal.timeout = window.setTimeout(function () {
    $scope.modal.interval = window.setInterval(function () {
        modalSubmit();
    }, 5 * 60 * 1000);
    modalSubmit();
}, 5 * 60 * 1000);
1
6 grudzień 2013, 08:34