Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS query resource once and use data throughout app

Tags:

angularjs

My App has several views,each view has a controller.

I have several API resources returning standard JSON arrays.Whenever a view changes,the resources are re-queried for new data which is fairly slow. I'd prefer to provide my controllers with the API resources without requerying each time.

Whats the best way to do this?

like image 887
David Avatar asked Dec 05 '25 23:12

David


1 Answers

If I understand correctly then this is what services are for. They're kind of like a central place for controllers share data.

I looked over the jsfiddle for the code used in this tutorial:

http://onehungrymind.com/angularjs-communicating-between-controllers/

var myModule = angular.module('myModule', []);
myModule.factory('mySharedService', function($rootScope) {
    var sharedService = {};

    sharedService.message = '';

    sharedService.prepForBroadcast = function(msg) {
        this.message = msg;
        this.broadcastItem();
    };

    sharedService.broadcastItem = function() {
        $rootScope.$broadcast('handleBroadcast');
    };

    return sharedService;
});

function ControllerZero($scope, sharedService) {
    $scope.handleClick = function(msg) {
        sharedService.prepForBroadcast(msg);
    };

    $scope.$on('handleBroadcast', function() {
        $scope.message = sharedService.message;
    });        
}

function ControllerOne($scope, sharedService) {
    $scope.$on('handleBroadcast', function() {
        $scope.message = 'ONE: ' + sharedService.message;
    });        
}

function ControllerTwo($scope, sharedService) {
    $scope.$on('handleBroadcast', function() {
        $scope.message = 'TWO: ' + sharedService.message;
    });
}

ControllerZero.$inject = ['$scope', 'mySharedService'];        

ControllerOne.$inject = ['$scope', 'mySharedService'];

ControllerTwo.$inject = ['$scope', 'mySharedService'];​

EDIT

For resource calls, I have a service right now that uses them like this. Sorry that its in coffeescript

.factory('EventService', (SubEvent, User) ->
    subevents = {}

    return {
        getSubevent: (subevent_id) ->
            SubEvent.get {subevent_id: subevent_id}, (subevent) ->

                participants = (participant.user for participant in subevent.participants)
                User.query {participants: participants}, (users) ->
                    for user,i in users
                        subevent.participants[i].user = user

                    subevents[subevent_id] = subevent
    }
)
like image 105
Justen Avatar answered Dec 07 '25 16:12

Justen