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

Get new records from the store #245

Closed
josepjaume opened this issue May 6, 2012 · 7 comments
Closed

Get new records from the store #245

josepjaume opened this issue May 6, 2012 · 7 comments

Comments

@josepjaume
Copy link

I guess I'm doing something wrong, but I've been looking all allong the documentation and the source code and I can't seem to find the proper way to do this using the REST adapter.

My scenario is simple: I have loaded all my app's contacts:

App.store.findAll(App.Contact)

Later on, I perform a POST to a particular location in my backend, which creates a few more contacts. Since the first request is cached, every action I perform (find, findAll, etc.) seems to return results from the cached Array instead of doing the round trip to the server.

What's the proper way to update the store with the new records from the backend?

Thanks and amazing work!

@tchak
Copy link
Member

tchak commented May 6, 2012

@josepjaume well for now, the only way to refresh records from server is to perform a findQuery

In a simple senario you should do App.Contact.createRecord({}) and them commit your store, this way your list of contacts in the store will be up to date.
It is probably not the best idea to bypass the store when manipulating your rest-api if you decide to use ember-data

@josepjaume
Copy link
Author

First of all, thanks for answering so fast!

I don't get exactly how should I perform that findQuery - could you post an example?

In my particular scenario, I can't (or at least I think so) do it the standard way. I'm implementing a contact importer that adds new contacts based on the contents of a CSV.

Maybe returning the new contacts in the response and then passing it to the store's loadMany would be the best way to handle it?

$.ajax({url: '/import/csv', data: data, dataType: 'json', success: function(results){
  App.store.loadMany(App.Contact, results);
});

@tchak
Copy link
Member

tchak commented May 6, 2012

Yep this would work, and I think In your case it is a good way to do it.

As for findQuery, for example you could do this :

App.store.findQuery(App.Contact, {name: 'toto'});

and it will call

/contacts?name=toto

This kind of requests are never cached by ember-data

@josepjaume
Copy link
Author

I've implemented the solution I proposed and it works, but I can confirm yours work too :D.

So, just for the record, those two options work:

App.store.findQuery(App.Contact, {});
App.store.loadMany(App.Contact, data.contacts);

I think I will stick with loading the new records because it seems more ember-idiomatic, but it's awesome to know the cache can be so easily overriden.

Thanks!

@tchak
Copy link
Member

tchak commented May 7, 2012

Can we close this? We do have an issue on reloading/loding more records stuff in #51

@josepjaume
Copy link
Author

Absolutely! Sorry about that, I looked through the issues but I couldn't find what I was looking for.

Thanks for your help.

@tobikko
Copy link

tobikko commented Jun 22, 2012

hello, i'm trying this approach

$.ajax({url: '/import/csv', data: data, dataType: 'json', success: function(results){
App.store.loadMany(App.Contact, results);
});

and i'm getting an error
"Uncaught Error: DS.StateManager:ember563 could not respond to event setData in state rootState.loaded. "

can you provide more details of how this works?

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

No branches or pull requests

3 participants