Carl Wilson
Carl Wilson

Reputation: 189

Prevent multiple ajax calls when re-using a controller/factory

just starting out really with Angular and need some advice regarding preventing repeated ajax requests for the same data when re-using a controller with multiple view.

So I have say 6 views all referencing the same controller but different views

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);
}());

So with this - every time a new view is loaded the ajax request is made again - how would I re-purpose to have only a single request happen?

As always thanks in advance

Carl

UPDATE: Answer marked below but I also had success by changing the "cache" config item/property (whatever its called) to true in the jsonp request

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

Upvotes: 3

Views: 1031

Answers (1)

Manuel Bitto
Manuel Bitto

Reputation: 5253

You could change your factory in this way:

(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);
}());

Now getSamples() returns a promise that you should manage in your controllers.

Upvotes: 1

Related Questions