Service not loaded when controller calls in init function

35 views
Skip to first unread message

Stephen Adams

unread,
Apr 1, 2015, 5:38:50 AM4/1/15
to ang...@googlegroups.com
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

Sander Elias

unread,
Apr 1, 2015, 11:08:10 AM4/1/15
to ang...@googlegroups.com

Hi Stephen,

No I have not witnessed something like this. Did you check the developers console on what errors are present when it’s not working?
A quick view of your source brings this line to my attention:

   this.load();

I suspect your issue will be gone if you change that to:

   dataAccessService.load();

I hope this helps you,
Regards
Sander

Stephen Adams

unread,
Apr 2, 2015, 4:42:22 AM4/2/15
to ang...@googlegroups.com

Hi

Thanks for this I'll try that change you suggested. Also I noticed that we're using appcache which seems to cause problems. I'm looking for ways to use appcache with an AngularJS/Ionic app.

Stephen

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/u7WtHaZ5lIo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages