question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ember-data: findAll() works correctly, query() ... not so much

See original GitHub issue

GitHub repository which concicesly recreates this problem:

Below is an explanation of the issue, and the following text also appears in the README of the repo above.

The Core of the Problem

Basically, the bug appears to be centered around the type of query used in app/routes/index.js to load the model for the view:

  • If I use .findAll(modelItemName) - it Works*, however if I do .query(modelItemName, {}) - it Does Not Work*.

What does Works and Does Not Work Mean?

Great question, glad you asked.

First, context: in app/templates/index.hbs, we have a VERY simple UI:

<button {{action 'addNewItem'}}> + Add New Item </button>
<ul>
{{#each model as |item|}}
  <li>{{item.name}}</li>
{{/each}}
</ul>

And the appros router action in app/routes/index.js does this:

addNewItem() {
  let item = this.get('store').createRecord(modelItemName, {
    name: new Date()
  });
  item.save();
}

So, to define the terms Works and Does Not Work:

  • Works: (e.g. what happens with .findAll()) When clicking the Add New Item button in the UI, triggering the addNewItem() action, the new item is automatically rendered in the {{#each}} block into the page.
  • Does Not Work: (e.g. what happens with .query()): When clicking the button and triggering addNewItem(), the UI never updates. The console shows Mirage POSTed the item and received a valid response (see screenshot below), but the {{#each}} block is never re-rendered.

No idea how to fix this. Suggestions and/or pointing out the error of my ways is very welcome and desired.

Screenshot

Here’s digital proof showing the console after the Add New Item button was clicked showing that Mirage did post the item but the UI never updated:

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:27 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
jaswillicommented, May 21, 2017

What you have up there is really close to working. Ember Arrays have two special properties: [], and @each that you can reference when using an Ember Array as a dependent key in a computed property.

In your example, if you want to update and re-sort every time the membership in the list of items changes, you can make sortedItems look like this:

sortedItems:  Ember.computed('items.[]', function () {
    return this.get('items').sortBy('name');
})

Or, if item.name may be updated by something and you want to re-sort when that happens (in addition to when an item is added/removed), you can use .@each:

sortedItems: Ember.computed('items.@each.name', function () {
    return this.get('items').sortBy('name');
})

edit I should add that I didn’t use [] or @each in my previous examples because the filterBy computed function sets up the correct observer for you.

3reactions
jaswillicommented, May 20, 2017

Sure, if you want a subset of the total available records when this particular route loads you’re free to use store.query() in the model hook to get what you need.

What would change with that strategy would be to then get a “live” RecordArray somewhere other than the model hook. For Example:

// route.js
model() {
  return this.store.query(modelItemName, {/* query */});
}

// controller.js
items: Ember.computed(function () {
  return this.store.peekAll(modelItemName);
},

filteredItems: Ember.computed.filterBy('items', 'name', 'test')

items is then a live view into the store and will update whenever a new modelItemName gets added.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Clear data from Ember's Store before doing findAll
so far, I have wrapped the unloadAll() in an Ember.run, and it appears to work, but I'm not sure why this is necessary:...
Read more >
store.findAll() is not working - Ember Data
I am looking to display the all the questions from by backend. The store.findAll() requests and gets the response from the endpoint succcessfully...
Read more >
Finding Records - Ember Data
findAll() returns a PromiseArray that fulfills to a RecordArray and store.peekAll directly returns a RecordArray . It's important to note that RecordArray is ......
Read more >
Ember Data - Part 2 - Ember Guides
In this chapter, we will work on removing some code duplication in our route handlers, by switching to using Ember Data to manage...
Read more >
JSONAPIAdapter: Pagination when using store.findAll
I'm aware that I can pass arbitrary parameters using query . If I understand it correctly findAll and query have different behaviors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found