vendredi 17 juillet 2020

EmberJS 3.19 PATCHes initial data instead of new data

I have a local-score model

export default class LocalScoreModel extends Model {
  @attr username;
  @attr wins;
  @attr draws;
  @attr losses;
}

I display this local-score model in a scoreboard template. This is the Scoreboard route

export default class ScoreboardRoute extends Route {
  async model() {
    return {
      localScore: this.store.findAll('local-score')
    }
  }
}

The data gets fetched fine and it's all as it should be, no problems there. However, I'm trying to test out some things and created a button that will increase or decrease the draws in my model.

The scoreboard controller looks like this

  @action
  changeDraw(amount) {
    this.store.findRecord("local-score", 1).then((score) => {
      score.draws += amount
      score.save();
    })
  }

What happens, though, is that while the score.wins does change its amount, upon hitting score.save(), for some reason it resets and what ends up happening is that Ember will send a PATCH with the original values instead of the new ones.

If I do something like this:

  @action
  changeDraw(amount) {
    this.store.findRecord("local-score", 1).then((score) => {
      console.log("Before += ", score.draws);
      score.draws += amount;
      console.log("After += ", score.draws);
      score.save();
      console.log("After save() ", score.draws);
    })
  }

The output is

Before +=  1000
After +=  1001
After save()  1001

Checking the Network tab when I click the button, I actually have 2 requests show up:

First, a GET request gets sent to the API. AFTER the GET, my PATCH gets sent, and obviously now the records go back to their original values which is why the PATCH sends out the old data.

What might be causing this initial GET request?




Aucun commentaire:

Enregistrer un commentaire