angularjs停止ajax微调器的一些请求

时间:2013-03-15 12:13:39

标签: ajax angularjs angularjs-service

我已经使用transformRequest和responseInterceptors设置了一个ajax微调器,如下所示 http://jsfiddle.net/zdam/dBR2r/

    angular.module('SharedServices', [])
    .config(function ($httpProvider) {
        $httpProvider.responseInterceptors.push('myHttpInterceptor');
        var spinnerFunction = function (data, headersGetter) {
            // todo start the spinner here

            alert('start spinner');
            return data;
        };
        $httpProvider.defaults.transformRequest.push(spinnerFunction);
    })
// register the interceptor as a service, intercepts ALL angular ajax http calls
    .factory('myHttpInterceptor', function ($q, $window) {
        return function (promise) {
            return promise.then(function (response) {
                // do something on success
                // todo hide the spinner
                alert('stop spinner');
                return response;

            }, function (response) {
                // do something on error
                // todo hide the spinner
                alert('stop spinner');
                return $q.reject(response);
            });
        };
    })

angular.module('project', ['mongolab', 'SharedServices']).
  config(function($routeProvider) {
    $routeProvider.
      when('/list', {controller:ListCtrl, template:'list.html'}).
      when('/edit/:projectId', {controller:EditCtrl, template:'detail.html'}).
      when('/new', {controller:CreateCtrl, template:'detail.html'}).
      otherwise({redirectTo:'/list'});
  });


function ListCtrl($scope, Project) {
  $scope.projects = Project.query();
}


function CreateCtrl($scope, $location, Project) {
  $scope.save = function() {
    Project.save($scope.project, function(project) {
      $location.path('/edit/' + project._id.$oid);
    });
  }
}


function EditCtrl($scope, $location, $routeParams, Project) {
  var self = this;

  Project.get({id: $routeParams.projectId}, function(project) {
    self.original = project;
    $scope.project = new Project(self.original);
  });

  $scope.isClean = function() {
    return angular.equals(self.original, $scope.project);
  }

  $scope.destroy = function() {
    self.original.destroy(function() {
      $location.path('/list');
    });
  };

  $scope.save = function() {
    $scope.project.update(function() {
      $location.path('/list');
    });
  };
}

// This is a module for cloud persistance in mongolab - https://mongolab.com
angular.module('mongolab', ['ngResource']).
    factory('Project', function($resource) {
      var Project = $resource('https://api.mongolab.com/api/1/databases' +
          '/angularjs/collections/projects/:id',
          { apiKey: '4f847ad3e4b08a2eed5f3b54' }, {
            update: { method: 'PUT' }
          }
      );

      Project.prototype.update = function(cb) {
        return Project.update({id: this._id.$oid},
            angular.extend({}, this, {_id:undefined}), cb);
      };

      Project.prototype.destroy = function(cb) {
        return Project.remove({id: this._id.$oid}, cb);
      };

      return Project;
    });

但我不会为了一些ajax调用而停止/隐藏ajax微调器。比如自动完成ajax调用。

有没有办法实现这个目标?感谢

1 个答案:

答案 0 :(得分:1)

我找到了来自angularjs google grops的解决方案,

https://groups.google.com/d/msg/angular/K8F_6K1C_GI/cOOEJPQC79cJ

正如James Morgan所说,在$ rootScope中放置一个标志并在显示微调器之前检查它。

angular.module('SharedServices', [])
.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('myHttpInterceptor');
    var spinnerFunction = function (data, headersGetter) {
        // todo start the spinner here
        // check $rootScope flag
        rootscope = angular.element(document).injector().get('$rootScope');
        if(rootscope.spinner == null || rootscope.spinner != false)
           alert('start spinner');
        else
           rootscope.spinner = true; // reset for other requests

        return data;
    };
    $httpProvider.defaults.transformRequest.push(spinnerFunction);
})

希望这有助于他人。

相关问题