0

I have the following function in my factory

getSource:function(){
            // If not connected to internet
            return LocalStorage.getObject("sourcesAll");
            // If connected to internet

            return SourceRest.getInstance().query().$promise.then(function(data){
                LocalStorage.setObject("sourcesAll",data);
                return data;
            });
        }

My SourceRest is just a $resource call

What i want is that if the user is connected to the internet, he downloads the latest data and saves it to his Localstorage and then return it.
If not connected take the data from the Localstorage and return it.

Return data with the promise is working fine

return SourceRest.getInstance().query()

Also returning from the localStorage (if something is saved) is working

return LocalStorage.getObject("sourcesAll");

Can i return my data from the ajax callback or is there another way to do that?

1 Answer 1

0

Got it working by using $q.defer()

// If not connected to internet
return LocalStorage.getObject("sourcesAll");
// If connected to internet

var result= $q.defer();

SourceRest.getInstance().query().$promise.then(function(data){
      LocalStorage.setObject("sourcesAll",data);
      result.resolve(data);
});
return result.promise;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.