2个控制器中的一个功能[JS]

时间:2013-07-26 18:30:54

标签: javascript json angularjs

这是我的问题:我这里有一个小JS程序,其中有两个控制器..我的问题是我需要两个控制器中的一个功能(data.duration)。因为有一次我需要功能本身的功能和一次摘要。顺便说一下,我通过JSON报告获取我的信息,如果它是一个特征或只是一个值,该函数会证明自己。当函数在代码中2次......

那么有没有办法在两个控制器中使用该功能?

    (function() {

     var loader = ['$http',
         function($http) {
             return $http.get('report.json')
                 .success(function(data) {

                     function getFailedScenarioCount(feature) {
                         var failedScenarios = 0;
                         feature.scenarios.forEach(function(scenario) {
                             if (scenario.result.failedStepCount)
                                 failedScenarios++;
                         });
                         return failedScenarios;
                     }


                     function getUnknownScenarioCount(feature) {
                         var unknownScenarios = 0;
                         feature.scenarios.forEach(function(scenario) {
                             if (scenario.result.unknownStepCount)
                                 unknownScenarios++;
                         });
                         return unknownScenarios;
                     }

                     angular.forEach(data.features, function(feature) {
                         if (feature.scenarios.length) {
                             feature.result.failedScenarioCount = getFailedScenarioCount(feature);
                             feature.result.unknownScenarioCount = getUnknownScenarioCount(feature);
                             feature.result.passedScenarioCount = feature.result.scenarioCount - feature.result.failedScenarioCount - feature.result.unknownScenarioCount;

                             if (feature.result.failedScenarioCount === 0)
                                 feature.result.failedScenarioCount = null;

                             if (feature.result.unknownScenarioCount === 0)
                                 feature.result.unknownScenarioCount = null;

                             if (feature.result.passedScenarioCount === 0)
                                 feature.result.passedScenarioCount = null;
                         }

                         feature.status = feature.result.failedScenarioCount ? "FAILED" : "OK";

                         angular.forEach(feature.scenarios, function(scenario) {

                             angular.forEach(scenario.steps, function(step) {
                                 if (step.result.status === "undefined")
                                     step.result.status = "unknown";
                             });

                             scenario.status = scenario.result.failedStepCount ? "failed" : (scenario.result.unknownStepCount ? 'unknown' : 'passed');

                         });

                     });



                     data.duration = function(feature) {
                         var day = 0;
                         var h = 0;
                         var min = 0;
                         var sec = 0;
                         var value = 0;

                         var substract = function(v, b) {
                             var result = v - (b);
                             return result;
                         };

                         if (isNaN(feature)) {
                             value = feature.result.duration;
                         } else {
                             value = feature;
                         }
                         console.log("value: " + value);
                         if (value % (1000000000 * 60 * 60 * 24) >= 0) //day 
                         {
                             day = (value / (1000000000 * 60 * 60 * 24)) | 0;
                             value = substract(value, (day * (1000000000 * 60 * 60 * 24)));
                         }
                         if (value % (1000000000 * 60 * 60) >= 0) //hour 
                         {

                             h = (value / (1000000000 * 60 * 60)) | 0;
                             value = substract(value, (h * (1000000000 * 60 * 60)));
                             if (h % 24 === 0 && h !== 0) {
                                 day++;
                                 h = 0;
                             }
                         }
                         if (value % (1000000000 * 60) >= 0) //minute 
                         {
                             min = (value / (1000000000 * 60)) | 0;
                             value = substract(value, (min * (1000000000 * 60)));
                             if (min % 60 === 0 && min !== 0) {
                                 h++;
                                 min = 0;
                             }
                         }
                         if (value % (1000000000) >= 0) //second 
                         {
                             sec = (value / (1000000000)) | 0;
                             value = substract(value, (sec * (1000000000)));
                             if (sec % 60 === 0 && sec !== 0) {
                                 min++;
                                 sec = 0;
                             }
                         }

                         if (day === 0 && h === 0 && min === 0 && sec === 0) {
                             var msec = 0;
                             if (value % 1000000 >= 0) {
                                 msec = (value / 1000000) | 0;
                             }
                             return msec > 0 ? msec + 'ms' : '<1ms';
                         } else if (day > 0) {
                             return (' ' + '(' + day + 'Day/s) ' + ' ' + ' ' + h + ' ' + ':' + ' ' + min + ' ' + ':' + ' ' + sec);
                         } else {
                             return (' ' + h + ' ' + ':' + ' ' + min + ' ' + ':' + ' ' + sec);
                         }
                     };

                 });
         }
     ];

     var app = window.angular.module('cucumber', ['ui.bootstrap']);

     app.config(['$routeProvider',
         function($routeProvider) {
             $routeProvider.when('/features', {
                 templateUrl: 'features.html',
                 controller: 'FeatureListCtrl',
                 resolve: {
                     report: loader
                 }
             }).when('/feature/:featureId', {
                 templateUrl: 'feature.html',
                 controller: 'FeatureCtrl',
                 resolve: {
                     report: loader
                 }
             }).otherwise({
                 redirectTo: '/features'
             });
         }
     ]);

     app.controller('FeatureListCtrl', function($rootScope, $scope, $location, $filter, report) {

         $scope.report = report.data;
         $rootScope.reportDate = $scope.report.date;
         $scope.duration = $scope.report.duration;

         $scope.featureDetails = function(feature) {
             $location.path('/feature/' + feature.id);
         };

         $scope.sum = function(features, field) {
             if (!features) return null;
             var sum = features.reduce(function(sum, feature) {
                 var value = parseInt(feature.result[field], 10);
                 return isNaN(value) ? sum : sum + value;
             }, 0);
             return sum > 0 ? sum : null;
         };


         $scope.$watch("searchText", function(query) {
             $scope.filteredFeatures = $filter("filter")($scope.report.features, query);
         });

         $scope.isCollapsed = true;

     });

     app.controller('FeatureCtrl', function($rootScope, $scope, $location, $routeParams, report) {

         $scope.duration = report.data.duration;

         function getFeature(featureId, features) {
             for (var i = 0; i < features.length; i++) {
                 if (featureId === features[i].id) {
                     return features[i];
                 }
             }
         }

         $scope.feature = getFeature($routeParams.featureId, report.data.features);
         $rootScope.reportDate = report.data.date;

     });
 }());

1 个答案:

答案 0 :(得分:1)

非常简单,编写服务并将其注入两个控制器。

app.service("utilityService", function () {
    return {
        duration: function (feature) {
            ...your code
        }
    }
});

然后将其注入每个控制器:

app.controller('FeatureListCtrl', function($rootScope, $scope, $location, $filter, report, utilityService) {

});

然后参考:

utilityService.duration($scope.whatEverData)