Hi,

I have a service which loads data from a external service and I call this 
in the init function of my controller. The problem is sometimes it appears 
as though my data access service has not loaded. This is the error message 
I get:

DataAccessService.load is not a function

My service is set up as a factory like this:

angular.module('MemberInduction').factory('DataAccessService', function (
$http, ConfigService) {


    var dataAccessService = {};


    function writeToLocalStorage(data) {
        localStorage.setItem('MI.ApplicationData', JSON.stringify(data));
        reloadView();
    }


    function reloadView() {
        window.location.href = '#/tab/home'
    }


    dataAccessService.load = function () {
        var tempData = {};
        var currentEnvironment = ConfigService.getEnvironment().environment;
        var loadApplicationDataUrl = '';
        if (currentEnvironment == 'dev') {
            loadApplicationDataUrl = ConfigService.getEnvironment().urls.dev
.baseUrl + ConfigService.getEnvironment().urls.dev.loadDataUrl;
        } else {
            loadApplicationDataUrl = ConfigService.getEnvironment().urls.
live.baseUrl + ConfigService.getEnvironment().urls.live.loadDataUrl;
        }
        $http.get(loadApplicationDataUrl).success(function (data, status, 
headers, config) {
            tempData = data;
            writeToLocalStorage(data);
            
        }).error(function (error) {
            console.log('error handler of dataAccessService Load', error);
        });
        return tempData; 
    }


   


    dataAccessService.resetLocalStorage = function () {
        localStorage.setItem('MI.ApplicationData', null);
        localStorage.setItem('MI.ApplicationFirstRun', false);
        this.load();
    };


    dataAccessService.getInductionList = function () {
        var dataFromlocalstorage = localStorage.getItem('MI.ApplicationData'
);
        var appData = JSON.parse(dataFromlocalstorage);
        return appData.Inductions;
    };


    dataAccessService.getInductionDetailsById = function (id) {
        var inductionsObj = this.getInductionList();
        var induction = null;
        for (var inductionItem in inductionsObj) {
            
            if (inductionsObj.hasOwnProperty(inductionItem)) {
                if (inductionsObj[inductionItem].InductionId == id) {
                    induction = inductionsObj[inductionItem];
                }
            }
        }
        return induction;
    };


    dataAccessService.getGroupList = function () {
        var dataFromlocalstorage = localStorage.getItem('MI.ApplicationData'
);
        var groups = JSON.parse(dataFromlocalstorage).Groups;
        return groups;
    };


    dataAccessService.getGroupDetailsById = function (id) {
        var listOfGroups = this.getGroupList();
        var selectedGroup = null;
        for (var currentGroup in listOfGroups) {
            
            if (listOfGroups.hasOwnProperty(currentGroup)) {
                if (listOfGroups[currentGroup].GroupId == id) {
                    selectedGroup = listOfGroups[currentGroup];
                }
            }
        }
        
        return selectedGroup;
    };


    dataAccessService.getGeneralInformation = function () {
        var data = JSON.parse(localStorage.getItem('MI.ApplicationData'));
        var generalInformation = data.GeneralInformation;
        return generalInformation;
    };


    return dataAccessService;
});


I have also tried using resolve in my ui-roter file, like this:

 $stateProvider
            .state('tabs', {
                url: '/tab',
                abstract: true,
                templateUrl: 'tabs.html'
            })
            .state('tabs.home', {
                resolve: {
                    DataAccessService: function (DataAccessService) {
                        return DataAccessService.load();
                    }
                },
                url: '/home',
                views: {
                    'home-tab': {
                        templateUrl: 'assets/views/inductionList.html',
                        controller: 'InductionListCtrl'
                    }
                }
            })

Sometimes it works, but sometimes I get this strange error, where it looks 
like DataAccessService has not fully loaded and the functions within it are 
not yet accessible.

Anyone seen a problem like this?

Thanks

Stephen

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to