Prevent multiple ajax calls when reusing controller / factory

just starting with Angular and need advice on preventing repeated ajax requests for the same data when reusing a controller with multiple views.

So I will say that 6 views all refer to 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 executed again - how could I reuse just one request?

As always in advance

Charles

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

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

      

+3


source to share


1 answer


You can change your factory 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 must control in your controllers.

+1


source







All Articles