I have 2 services. One service (TimeEventService) saves data to a database table. The other service (ReportingService) is a wrapper for TimeEventService and provides functionality to controllers.
I need the ReportingService to wait for a 'saved' response from the TimeEventService. What is the best and simplest way to do this please.
TimeEventService code:
angular.module('TimeEventServiceMod', ['ngResource']).
factory('TimeEventService', function(TestTimeEventService,$filter) {
var timeEventData = {
savedEvent:null
};
timeEventData.newEvent = function(ataskSequenceId,eventNumber){
var newEvent = new TestTimeEventService();
newEvent.tasksequenceid = ataskSequenceId;
newEvent.event = eventNumber;
newEvent.timeofevent = new Date();
newEvent.$save(function(){
timeEventData.newEvent=newEvent;
console.log('saved event OK: ' + angular.toJson(timeEventData.newEvent));
return timeEventData;
},function (errorResponse) {
// failure callback
console.log('error saving Event and new status to db:' + angular.toJson(errorResponse));
});
};
ReportingService code:
angular.module('ReportingServiceMod', ['ngResource','TimeEventServiceMod']).
factory('ReportingService', function(TimeEventService) {
var reportData = {
lastTimeEventId:-1
};
var timeEventData = TimeEventService;
//the public test events
reportData.newEvent_testStart = function(ataskSequenceId){newEvent(ataskSequenceId,10)};
newEvent = function(ataskSequenceId, eventid){
timeEventData = TimeEventService.newEvent(ataskSequenceId,eventid);
if (timeEventData.savedEvent!=null)
reportData.lastTimeEventId = timeEventData.savedEvent.id;
console.log('saved time event id:' + reportData.lastTimeEventId);
};
return reportData;
})
Note the line (if (timeEventData.savedEvent!=null)) does not work as the object has not yet been saved.
savedEventactually get assigned? I don't see this happening anywhere.