angular2-jsonapi icon indicating copy to clipboard operation
angular2-jsonapi copied to clipboard

Relationships are not updated when fetching a model

Open clementprdhomme opened this issue 7 years ago • 1 comments

Hi!

The issue is a bit complicated to explain without an example so I'll go straight into it. Let's say I have two models:

law.model.ts

@JsonApiModelConfig({
  type: 'laws'
})
export class Law extends JsonApiModel {
  @Attribute() name: string;
  @BelongsTo() country: Country;
}

country.model.ts

@JsonApiModelConfig({
  type: 'countries'
})
export class Country extends JsonApiModel {
  @Attribute() name: string;
}

Now, my main code fetches the laws with a locale parameter:

this.datastore.query(Law, { locale: 'en' })
  .then(laws => console.log(laws));

I get something like this:

+-------+----------------+
| Name  | Country        |
+-------+----------------+
| Law 1 | Spain          |
+-------+----------------+
| Law 2 | United Kingdom |
+-------+----------------+

Then, at some point, I fetch them again but with a different locale:

this.datastore.query(Law, { locale: 'fr' })
  .then(laws => console.log(laws));

I would expect:

+-------+----------------+
| Name  | Country        |
+-------+----------------+
| Law 1 | Espagne        |
+-------+----------------+
| Law 2 | Royaume-Uni    |
+-------+----------------+

but in reality, the countries still have the English names because the relationships are not updated.

I searched a bit in the code and found that this line is responsible for the issue. Basically, because the store already has the countries, it re-uses them.

Any idea of a workaround?

Clément

PS: I currently use the v.3.6.0.

clementprdhomme avatar Nov 06 '17 11:11 clementprdhomme

Can you confirm if this is still an issue with the current version?

HennerM avatar Feb 11 '18 20:02 HennerM