Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.find skips the object in the store #117

Closed
demisx opened this issue Apr 11, 2015 · 7 comments
Closed

.find skips the object in the store #117

demisx opened this issue Apr 11, 2015 · 7 comments

Comments

@demisx
Copy link

demisx commented Apr 11, 2015

I am using Angular 1.4 with JS-Data 1.6.3 and I am facing a weird situation. I have an object in the data store:

DS.get('section', 2); // -> { id: 2, code: "ppp", ...}

However, find for the same object issues an HTTP request to the server instead of retrieving it from DS:

DS.find('section', 2); // -> http://api.example.org:3000/v1/sections/1

What would prevent it from getting the object from the data store first? After all, I am not passing { bypassCache: true }. Thanks.

@demisx demisx changed the title Find skips the object in the store .find skips the object in the store Apr 11, 2015
@jmdobry
Copy link
Member

jmdobry commented Apr 11, 2015

How did the item get into the store in the first place?

@demisx
Copy link
Author

demisx commented Apr 11, 2015

It was injected from hasMany relationship defined on the parent classified resource. However, this is a many-to-many relationship (classifieds <-> sections) and I had it defined as one-to-many. Could this be the cause? I thought that .find would retrieve the item from the store as long as the name and ID match. Sorry, I haven't had a chance to look at the relevant JS-Data source code yet.

@jmdobry
Copy link
Member

jmdobry commented Apr 11, 2015

You're seeing this behavior because find not only inject an item, but it marks a "completedQuery" for the item, which is also checked when deciding whether to resolve immediately with the item and skip the http request. inject by itself does not set this completed query. I should probably make it configurable so you can skip the completedQuery check and just rely on the item already being in the store from inject.

You can manually set the completedQuery, but that's dumb. Using a User resource as an example:

var user = User.inject({ id: 1});
store.store.user.completedQueries[user.id] = new Date().getTime();
User.find(1).then(function (user) {
  // no http request was made
});

@demisx
Copy link
Author

demisx commented Apr 11, 2015

Just took a quick glance at the code. I see that this section object (id: 2) is not in the resource.completedQueries, so the promise is not resolved with DS#get call.

@demisx
Copy link
Author

demisx commented Apr 11, 2015

Looks like we've posted pretty much at the same time. Yes, I would like find to be able to retrieve the item from the store even if this item was injected from relation and not direct query. After all, this object is the same and there is no need to query the server. I'll see if I can work around with #get instead, maybe.

@jmdobry
Copy link
Member

jmdobry commented Apr 11, 2015

I'll come up with a way to make this more configurable.

@demisx
Copy link
Author

demisx commented Apr 11, 2015

I have no doubt you will. 👍 Thank you for being awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants