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.

Computed properties on `hasMany` relationships don't update after model saves

See original GitHub issue

With Ember 3.0.0 (and also with 3.1.0-beta.1 and canary), but not with Ember <= 2.18.2, filterBy on a hasMany relationship aren’t being recomputed when a property changes on one of the underlying models.

Reproduced in a simple new app: https://github.com/pgengler/ember-update-bug. The app consists of two <ul>s, one displaying “saved” models (isNew=false) and the other displaying “pending” models (isNew=true), and a button that creates a new model and saves it. While it’s saving, it should appear in the “Pending” section and once the POST completes (and isNew gets sets to false on the model) it should then appear in the “Saved” section.

With Ember >= 3.0.0, a newly-created thing never moves from “Pending” (with <= 2.18.2 it works as expected). If I do some logging like this:

      let task = this.get('store').createRecord('task', {
        description: 'new task',
        list: this.get('list')
      });
      next(function() {
        task.save()
          .then(() => console.log('task.isNew: %s', task.get('isNew'))
          .then(() => console.log('tasks.mapBy(isNew): %O', list.get('tasks').mapBy('isNew'));
      });

I see that isNew for the new task has changed to false (as expected) but the mapBy returns [true].

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dwickerncommented, Feb 19, 2018

It’s the same issue as https://github.com/emberjs/data/pull/5359

The workaround there is to set up the relationship after calling createRecord

addTask() {
  let task = this.get('store').createRecord('task', {
    description: 'new task',
    // list: this.get('list')
  });
  task.set('list', this.get('list'));
  next(() => task.save());
}
1reaction
matthookscommented, Jun 21, 2018

Just wanted to comment I’m still seeing this behavior in Ember 3.2.1 / Ember Data 3.1.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ember computed property doesn't update when dependent ...
The logic is simple, the model(meal list) is passed in food-input , the computed property newNumber gets the last meal number and plus...
Read more >
Ember Computed Property on hasMany relationship not ...
[Solved]-Ember Computed Property on hasMany relationship not updating and doubles on ... After saving the model, I manually reset the shipmentPrice to 0...
Read more >
Relationships - Models - Ember Guides
The naïve solution would be to define models for each nested object and use hasMany and belongsTo to recreate the nested relationship.
Read more >
Properties | Laravel Livewire
Binding Nested Data; Debouncing Input; Lazy Updating; Deferred Updating. Binding Directly To Model Properties; Custom (Wireable) Properties; Computed ...
Read more >
API Reference - Bookshelf.js
The name to save the model as, or the name of the model to retrieve if no further ... which can handle components...
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