重新使用控制器/工厂时,防止多个ajax调用

时间:2014-08-23 13:00:00

标签: angularjs

刚刚开始使用Angular,需要一些关于在重新使用具有多个视图的控制器时防止对相同数据的重复ajax请求的建议。

所以我说6个视图都引用了相同的控制器,但视图不同

app.js

(function() {
var app = angular.module('myApp', ['ngRoute','ui.unique']);

app.config(function ($routeProvider) {
    // Routes
    $routeProvider
        .when('/',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/home.html'
            })
        .when('/view2/',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/main.html'
            })
        .when('/view3/:rangeName',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/samples.html'
        })
        .when('/view4/:rangeName',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/samples.html'
        })
        .when('/view5/',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/basket.html'
        })
        .when('/view6/',
            {
                controller: 'SamplesController',
                templateUrl: 'app/views/lightbox.html'
        })
        .otherwise({ redirectTo: '/' });
});
}());

samplesController.js

(function() {
var SamplesController = function ($scope, SamplesFactory, appSettings, $routeParams) {

    function init() {
        // back function
        $scope.$back = function() { 
            window.history.back();
        };

        // app settings
        $scope.settings = appSettings;

        // samples list
        SamplesFactory.getSamples()
            .success(function(data){
                var returnSamples = [];
                    for (var i=0,len=data.length;i<len;i++) {
                        if (data[i].range === $routeParams.rangeName) {
                            returnSamples.push(data[i]);
                        }
                    }
                $scope.samples = returnSamples;
            })
            .error(function(data, status, headers, config){
                // return empty object
                return {};
            });

        // variables for both ranges
        $scope.rangeName = $routeParams.rangeName;

        // click to change type 
        $scope.populate = function(type) {
               $scope.attributeValue = type;
        };
    };

    init();
};

SamplesController.$inject = ['$scope','SamplesFactory', 'appSettings', '$routeParams'];

angular.module('myApp').controller('SamplesController', SamplesController);
}());

samplesFactory.js

(function () {
var SamplesFactory = function ($http) {

var factory = {};

factory.getSamples = function() {
    return $http.jsonp('http://www.website.com/app/index.php?callback=JSON_CALLBACK');
};

return factory;
};
SamplesFactory.$inject = ['$http'];
angular.module('myApp').factory('SamplesFactory', SamplesFactory);
}());

所以有了这个 - 每次加载一个新视图时,都会再次发出ajax请求 - 我将如何重新设置只发生一个请求?

一如既往地感谢

卡尔

更新:以下标记的答案,但我也通过更改&#34;缓存&#34;在jsonp请求中将配置项/属性(无论其调用)设置为true

return $http.jsonp('http://www.website.com/app/index.php?callback=JSON_CALLBACK',{cache: true});

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式更改工厂:

(function () {
    var SamplesFactory = function ($http) {

    var factory = {},
        samples = $http.jsonp('http://www.website.com/app/index.php?callback=JSON_CALLBACK');


    factory.getSamples = function() {
        return samples;
    };

    return factory;
    };
    SamplesFactory.$inject = ['$http'];
    angular.module('myApp').factory('SamplesFactory', SamplesFactory);
}());

现在getSamples()会返回您应该在控制器中管理的承诺。