I'm building a shop in Ember with the list of products that are being added to Local Storage when the user clicks on the 'add to cart' button. Each product is an object that has a property called 'ordered_quantity', I'm trying to change this property's value when the user tries to remove the product from the cart. (example: 'ordered quantity: 8', when the button is clicked it should be 7). I have the following code in my service file:
remove(item) {
let new_arr = this.get('items');
let elementIndex = new_arr.findIndex(obj => {
return obj.id === item.id;
});
if (elementIndex !== -1) {
new_arr[elementIndex].ordered_quantity = new_arr[elementIndex].ordered_quantity - 1;
}
this.set('cart.items', new_arr);
}
I'm using Local Storage add-on (https://github.com/funkensturm/ember-local-storage#methods)
and I have the following action:
actions: {
removeFromCart(){
this.get('cart').remove(this.product);
}}
When I try to run the following code I get an error: Uncaught Error: Assertion Failed: You attempted to update [object Object].ordered_quantity to "7", but it is being tracked by a tracking context, such as a template, computed property, or observer. In order to make sure the context updates properly, you must invalidate the property when updating it. You can mark the property as @tracked
, or use @ember/object#set
to do this.
I tried using the set function like this:
let updated = item.ordered_quantity - 1;
set(item, 'ordered_quantity', updated);
https://api.emberjs.com/ember/release/functions/@ember%2Fobject/set and the code worked with no errors as expected, but the value of my property 'ordered_quantity' was not updated in the Local Storage.
Aucun commentaire:
Enregistrer un commentaire