Prima di tutto, come ha detto Nordyke nella sua risposta, è meglio dividere il servizio a quelli più piccoli.
In secondo luogo, stai chiedendo come memorizzare nella cache i dati e dal momento che stai usando Promise, $q.when()
è ciò di cui hai bisogno. Prenderò il getPlatform
Come esempio per iniziare:
.factory('DataService', function($http, $q, $timeout) {
var os; // this variable is used to store the result
return {
getPlatform: function() {
var getPlatformData = function() {
if (!os) { // no previous data available, look into other service to fetch the data
var deferred = $q.defer();
BrowserDetect.init();
os = BrowserDetect.OS; // store data
deferred.resolve(os);
return deferred.promise;
}
return $q.when(os); // there is previous data, return it as promise
};
return {
getPlatformData: getPlatformData
};
}
};
});
In questo modo, le informazioni sul sistema operativo sono memorizzate nella cache e
DataService.getPlatform().getPlatformData().then(function(platform) {
...
});
Prenderà le informazioni della piattaforma solo una volta durante la vita del dati dati. Puoi applicare la stessa idea a getServerData
Inoltre per memorizzare nella cache i dati dal server.