jmoreno
jmoreno

Reputation: 612

Angular factory with multiple data sources

I'm working on a project that includes a model that gets it data from different json files depending on the selected environment. There is one data_dev.json, data_sit.json, data_prd.json... you get the idea.

Currently I have this factory, to get the data, only one env supported.

angular.module('app').factory('Routing',function ($http, $q) {
    return {
        index: function() {
            var defer = $q.defer();

            $http.get('data/routing_index_prod.json').success(function (data) {
                defer.resolve(data);
            }).error(function () {
                defer.reject('Could not find ial_routing_index.json');
            });

            return defer.promise;
        },
    }
});

An then in a controller:

scope.environment = AppData.get('environment')
...
Routing.index().then(function (data) {
    scope.routingIndex = data;
})

I would like to tell the factory which environment I'm working on, so I can load the desired data file. How can I do that? Is the factory the best solution for this?

Thank you

Upvotes: 1

Views: 255

Answers (2)

mcs_dodo
mcs_dodo

Reputation: 738

You can store the urls and all environment related data in a separate factory, from which you can retrieve them, depending on what environment is returned from that AppData.get('environment') call of yours.

    angular.module('app').factory('Routing', 'envSettings', function ($http, $q, envSettings) {
    return {
        index: function() {
            var defer = $q.defer();

            var env = AppData.get('environment');

            $http.get(envSettings.getSettings(env)).success(function (data) {
                defer.resolve(data);
            }).error(function () {
                defer.reject('Could not find ial_routing_index.json');
            });

            return defer.promise;
        },
    }
});

angular.module('app').factory('envSettings',function () {
    var settings = {
        "dev" : { 
            url: 'whatever.json'
        },
        "prd" : {
            url: 'data/routing_index_prod.json'
        }
    };
    return {
        getSettings: function(environment) {
            return settings[environment];
        },
    }
});

Upvotes: 1

devqon
devqon

Reputation: 13997

Just pass in the variable to the index function:

angular.module('app').factory('Routing',function ($http, $q) {
    return {
        index: function(environment) {
            var defer = $q.defer();

            $http.get('data/' + environment + '.json').success(function (data) {
                defer.resolve(data);
            }).error(function () {
                defer.reject('Could not find ' + environment + '.json');
            });

            return defer.promise;
        },
    }
});

controller:

scope.environment = AppData.get('environment')

Routing.index(scope.environment).then(function (data) {
    scope.routingIndex = data;
})

Upvotes: 2

Related Questions