0

In my controller, I have a service call at 30 min of interval. There is a request parameter of the service. I want to cancel the response, if the request parameter is been changed between the time-span of service request-call and the response.

I am able to solve it using resolved method of $q, by writing the $http call in controller. But I have to maintain a separate service file for $http services.

Here is my controller :

app.controller('DCSubSystemCtrl', ['$scope', '$rootScope', '$localStorage', '$filter', '$interval', 'NgTableParams', 'operatorDCSubSysFactory', 'epochTime', '$timeout', 'operatorDashboardFactory',
  function($scope, $rootScope, $localStorage, $filter, $interval, NgTableParams, operatorDCSubSysFactory, epochTime, $timeout, operatorDashboardFactory) {
    $scope.perfoemanceGraph = function(blockID) {
      var reqObj = {
        "plantId": "IN.RJ.ABG.BHDL",
        "siteId": "IN.RJ.ABG.BHDL.Site1",
        "blockId": blockID,
        "subsystem": ""
      }
      operatorDashboardFactory.getWeatherFeed(reqObj).then(function(response) {
        if (response.status == 200) {
          $timeout($scope.perfoemanceGraph, 300 * 1000);
          $scope.weatherFeed = [];

          $scope.weatherFeed = response.data;
          console.log("Bye")
        } else {

        }
      });
    };
  }
]);
app.factory('operatorDCSubSysFactory', function($http, $log, $q, $resource, $location) {
  var baseUrl = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
  return {
    'getInverterBlocks': function(uriData) {
      ̶v̶a̶r̶ ̶d̶e̶f̶e̶r̶ ̶=̶ ̶$̶q̶.̶d̶e̶f̶e̶r̶(̶)̶;̶
      ͟r͟e͟t͟u͟r͟n͟ $http({
        headers: {
          'Content-Type': 'application/json'
        },
        data: uriData,
        method: 'POST',
        url: baseUrl + "/operator/dcsubsystem/inverterblock"
      }) ̶.̶t̶h̶e̶n̶(̶f̶u̶n̶c̶t̶i̶o̶n̶(̶s̶u̶c̶c̶e̶s̶s̶)̶ ̶{̶
        ̶d̶e̶f̶e̶r̶.̶r̶e̶s̶o̶l̶v̶e̶(̶s̶u̶c̶c̶e̶s̶s̶)̶;̶
      ̶}̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶e̶r̶r̶o̶r̶)̶ ̶{̶
        ̶d̶e̶f̶e̶r̶.̶r̶e̶j̶e̶c̶t̶(̶e̶r̶r̶o̶r̶)̶;̶
      ̶}̶)̶;̶
      ̶r̶e̶t̶u̶r̶n̶ ̶d̶e̶f̶e̶r̶.̶p̶r̶o̶m̶i̶s̶e̶;̶
    };
  };
});
georgeawg
  • 48,608
  • 13
  • 72
  • 95
SSS
  • 303
  • 1
  • 2
  • 9
  • $timeout() returns a promise that has a cancel() method. I would think that if you assign it to a $scope variable like, $scope.to = $timeout($scope.perfoemanceGraph, 300 * 1000) you could cancel it when the request parameter changes before calling $scope.perfoemanceGraph() with the new parameter. – jbrown Aug 21 '17 at 12:41
  • 1
    `$http` returns a Promise, use that instead of `$q` like `return $http({.....})` and `$scope.perfoemanceGraph` a parameter so pass that like `$timeout($scope.perfoemanceGraph, 300 * 1000, blockID)`. __What exactly you need?__ – Satpal Aug 21 '17 at 12:41
  • While not the solution to the problem, remove the [$q defer anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). It is unnecessary and prone to erroneous implementation. – georgeawg Aug 21 '17 at 12:56

0 Answers0