我可以通过电话启动民意调查服务吗? AngularJS

时间:2016-12-27 11:55:04

标签: javascript angularjs polling

我已经在AngularJS中编写了一个轮询服务,如果我的帖子请求完成,我想启动该服务。但是如果我调用gui,则轮询服务处于活动状态。

我已经尝试实现一个启动函数,结束函数并在post请求完成后调用start()函数..但它不起作用:/

我的民意调查服务:

.factory('NotificationPollService',
['$http', '$q', '$interval',
function ($http, $q, $interval) {

    var deferred = $q.defer();
    var notification = {};

    notification.poller = $interval(function(id) {
        $http.get('http://localhost:9999/v1/jmeter/id', {cache: false})
        .success(function(data, status, headers, config) {
             return data;
        }, 10000);
    });

    notification.endPolling = function() {$interval.cancel(this.interval);};    

}])

和我发布请求的控制器

.controller('HomeController',
['$scope', '$rootScope', 'SendJmeterFile', 'NotificationPollService',
function ($scope, $rootScope, SendJmeterFile , NotificationPollService) {

    $scope.upload = function() {

        var customArtifacts = "";
        var testDataBase = "";

        if($scope.jmeterFile.customArtifact == undefined){
            customArtifacts = null;
        } else {customArtifacts = $scope.jmeterFile.customArtifact.base64}

        if($scope.jmeterFile.testDataBase == undefined){
            testDataBase = null;
        } else {testDataBase = $scope.jmeterFile.testDataBase.base64}

        SendJmeterFile.upload($scope.jmeterFile.jmeter.base64, customArtifacts, $scope.jmeterFile.customProperties, $scope.jmeterFile.instanceConfiguration, $scope.jmeterFile.instances, $scope.jmeterFile.providerID, testDataBase)
            .then(function(data) {
                alert("Daten erfolgreich verschickt!");
                console.log(data);  
                NotificationPollService.poller(data.id)
                //.then(function(data) {
                    /*if(data.status == "SETUP")

                    if(data.status == "TEST")

                    if(data.status == "DONE")

                    if(data.status == "ERROR")
                }), function(data) {
                    })*/
            }, function(data) {
                alert("Fehler!");
                console.log(data);
            });     
    };
}])

2 个答案:

答案 0 :(得分:0)

您尝试拨打NotificationPollService.poller(data.id) Promise,实际上是因为NotificationPollService之前已经notification.poller分配了notification.poller = $interval(function(id) { $http.get('http://localhost:9999/v1/jmeter/id', {cache: false}) .success(function(data, status, headers, config) { return data; }, 10000); });

notification.poller

现在,您的$intervalpublic IActionResult WeeklyInvoiceNumber() { int id = 1; var whtev = _invNumRepo.GetSingle(i => i.id == id); InvoiceWeekViewModel vm = new InvoiceWeekViewModel(); vm.InvoiceNum = whtev.InvoiceNum; var help = vm.InvoiceNum; int invoice = ++help; if (DateTime.Today.DayOfWeek == DayOfWeek.Tuesday) { vm.InvoiceNum++; } if (vm.InvoiceNum == invoice) { InvoiceNumber InvNum = whtev; if (ModelState.IsValid && InvNum != null) { InvNum.InvoiceNum = vm.InvoiceNum; _invNumRepo.Update(InvNum); } } return View(vm); } 函数的返回值。 为了使它工作,你应该包装函数,以便你可以实际传递一个id。

答案 1 :(得分:0)

一个问题是注入控制器后会立即调用$interval()。你实现“开始”方法或类似方法的预感很好 - 但你可以通过让工厂返回一个函数来进一步简化它。然后,您可以在控制器中多次实例化该函数,因为您需要使用Poller。

然而,还有更多问题。承诺只能解决一次,并且由于您多次执行HTTP请求,我的猜测是您希望被“通知”状态更改,直到状态被标记为“完成”。您目前负责使用控制器检查状态。如果您想要通知所有“错误”和“成功”步骤,那么让Poller服务负责解释从您的服务返回的状态信息可能要好得多,并且仅仅依赖于关于控制器中的标准承诺行为。我选择展示后一种情况的一个例子:

更新:示例plnkr:http://plnkr.co/edit/e7vqU82fqYGQuCwufPZN?p=preview

angular.module('MYMODULENAMEHERE')
.factory('NotificationPoller',
    ['$http', '$q', '$interval',
    function ($http, $q, $interval) {

        return poller;

        function poller(id) {
            var _this = this;
            var deferred = $q.defer();

            var interval = $interval(function() {
                $http
                    // I assumed you actually want to use the value of 'id' in your
                    // url here, rather than just the word 'id'.
                    .get('http://localhost:9999/v1/jmeter/' + id, {cache: false})
                    .then(function(data, status, headers, config) {
                        // I commented out the following statement. It is meaningless because
                        // you can't do anything with the return value since it's an anonymous
                        // function you're returning it from. Instead, you probably meant to 
                        // use the promise to return the data.

                        // return data;

                        if(data.status == "SETUP") {
                            deferred.notify(data);
                        }
                        else if(data.status == "TEST") {
                            deferred.notify(data);
                        }
                        else if(data.status == "DONE") {
                            _this.endPolling(); // Automatically stop polling when status is done
                            deferred.resolve(data);
                        }
                        else { // data.status == "ERROR" (or anything else that's not expected)
                            _this.endPolling(); // Automatically stop polling on error
                            deferred.reject(data);
                        }                       
                    }, function(data) {
                        _this.endPolling();
                        deferred.reject(data);
                    });
            }, 10000);

            this.endPolling = function() {
                $interval.cancel(interval);
            };

            // Make the promise available to calling code
            this.promise = deferred.promise;
        };
}])

现在您的控制器可以更轻松地使用您的轮询服务。以下是使用轮询服务的精简控制器的示例,为清晰起见:

angular.module('MYMODULENAMEHERE')
.controller('HomeController', [
    'NotificationPoller',
    function(NotificationPoller) {

        var some_id = 207810;

        var poller = new NotificationPoller(some_id);
        poller.promise.then(onSuccess, onError, onNotify);

        function onSuccess(data) {
            // data.status == "DONE"
        };

        function onError(data) {
            // data.status == "ERROR"
        };

        function onNotify(data) {
            // data.status == "TEST" || data.status == "SETUP"
        };

    }]);

如您所见,工厂以这种方式承担了更多责任,但您的控制器无需了解后端可以发送的所有状态的详细信息。它只是使用标准的承诺。