angular-jsonapi icon indicating copy to clipboard operation
angular-jsonapi copied to clipboard

Retrieve data only from cache without request

Open eduardmartinez opened this issue 9 years ago • 5 comments

Hi.

More than a issue is a question. I noticed when I execute resource's all() method, it makes a request to the REST server automatically, even if I only want to take data from cache. For example:

// Factory

factory.publicData = [];

// Store in collection what it found in cache but makes the request anyway
var collection = resource.all();

// Make the public assignment to be used by controllers or another factory
factory.publicData = collection.data;

// if I want the updated data, I have to use the promise but the request was made
// even if I didn't want to use the promise
collection.promise.then(
  function success() {
     factory.publicData = collection.data;
  }
);

I want to avoid a lot of unnecessary requests to REST server because my app is big. Is there a way to use all() method without making the request and retrieving only cache data?

eduardmartinez avatar Oct 30 '15 14:10 eduardmartinez

@eduardmartinez it is a normal behaviour, but your flow is also reasonable. There is a little bit hakish solution for the current package version:

var collection = AngularJsonAPICollection.create(
        resource,
        params //can be omitted for default params
      );

But if you are not in a big hurry, I'll publish next alpha version today with an extra option params.local, to enable resource .all() and resource.get() without external calls. Then you can use it that way:

var params = {
//   (...) other params like filter etc.
    local: true
};
var collection = resource.all(params);

jakubrohleder avatar Oct 30 '15 14:10 jakubrohleder

@jakubrohleder That would be great! I can wait, hehe. It maybe would avoid me to store in memory each collection. But that would apply to collection methods too?

eduardmartinez avatar Oct 30 '15 14:10 eduardmartinez

Well technically speaking yes, but on the other hand what is the purpose of refreshing/fetching collection just locally?

I'd rather recommend you to keep reference to collection and use it in the view/controller by accessingcollection.data. So angular can watch for data changes and update your views each time the collection is refreshed/extended by new object etc.

You can see how I've done it in the demo/requests/all.controller.js the collection comes from resolve step in demo/index.js:44.

jakubrohleder avatar Oct 30 '15 14:10 jakubrohleder

Yeah, you're right, keeping reference of collection is better. Thank you and I'll wait for your update.

eduardmartinez avatar Oct 30 '15 15:10 eduardmartinez

@eduardmartinez it is a bit harder then I thought to make this change scalable. Just to let you know I may finish this fix tomorrow.

jakubrohleder avatar Oct 30 '15 18:10 jakubrohleder