sarunast
sarunast

Reputation: 2443

Share the same data between few controllers using ngResource

I am new to angular so it is probably easy question. I have this factory resource at the moment:

angular.module('resources.survey', ['ngResource'])
    .factory('Survey', function ($resource) {
        return $resource('/backend/surveys/:surveyId/data', {surveyId: '@id'});
    });

Controller:

.controller('PagesCtrl', function (Survey) {
        var survey = Survey.get({surveyId: 2});
        //now I want to change survey variable and share it between two controllers
 });

There are no problems with ngResource I can get the data from server. However I want to manipulate with the data from the server and use the same data in other controllers (probably using DI) and allow data manipulation there as well. I know that it can be done with $rootScope, but I was wondering if there is any other way.

Upvotes: 2

Views: 371

Answers (4)

sarunast
sarunast

Reputation: 2443

I managed to create a resource that can handle what I wanted. It is probably not as advanced as Chandermani suggested. But it works for my needs.

angular.module('resources.survey', ['ngResource'])
    .factory('Survey', function ($resource) {

        var resource = $resource('/backend/surveys/:surveyId/data',
            {surveyId: '@id'}
        );
        var Survey = {};
        var data = []; //saves the data from server 
        Survey.get = function(surveyId) {

            if(angular.isDefined(data[surveyId])){
                return data[surveyId];
            }

            return data[surveyId] = resource.get({surveyId: surveyId});
        };

        return Survey;
    });

And to call basically I call it like this:

.controller('QuestionsCtrl', function (Survey) {
    Survey.get(1).newData = 'newData'; //adding additional data
    console.log(Survey.get(1));
}); 

I guess this can be improved.

Upvotes: 0

Chandermani
Chandermani

Reputation: 42669

Your service should cache the response for the resource request in something like array of surveys and dispense surveys from this array instead of directly returning a resource object.

Controllers would only share data if the same reference for the survey is returned.

Roughly it would look like

.factory('Survey', function ($resource,$q) {
        var surveys[];
        return {
            getSurvey:function(id) {
                var defer=$q.defer();
                //if survery contains the survey with id do //defer.resolve(survey[i]);
                // else query using resource. On getting the survey add it to surveys result and resolve to the newly added survey.
            }
        }
    });

Upvotes: 1

igorzg
igorzg

Reputation: 1506

Here is complete documentation and example how to use it: http://docs.angularjs.org/api/ngResource.$resource

Upvotes: 0

Whisher
Whisher

Reputation: 32766

angular.module('resources.survey', ['ngResource'])
    .factory('Survey', function ($resource) {
        return $resource('/backend/surveys/:surveyId/data', {surveyId: '@id'});
    })
    .controller('MyCtrl', function($scope,Survey){
        //here you can call all the $resource stuff        
    });

Upvotes: 0

Related Questions