i have an attribute that is an array of objects , myArray: attr('array')
, rollbackAttributes()
is not applying on this attribute , is there any ready way to do it or should i implement something to handle it ? thank you.
@action
deleteItem(index) {
this.container.myArray.removeAt(index);
}
... if not saved => this.container.rollbackAttributes();
ember-data : 3.24
i have solved it by manually saving a copy of it, in my case this is working, does this solution present any inconvenient for more general cases ?
// my-model.js
init() {
this._super(...arguments);
if(this.get('myArray').length > 0){
this.set('_myArray', this.get('myArray').slice());
}
},
rollbackMyArray() {
const _myArray = this.get('_myArray');
this.set('myArray', [..._myArray]);
},
rollbackAttributes() {
this._super(...arguments);
this.rollbackMyArray();
},
This is the last version working for my case, i hope it will help someone.
init() {
this._super(...arguments);
this.cacheMyArray();
},
cacheMyArray(){
this.set('_myArray ', []);
let myArray = this.get('myArray');
if(myArray) this.set('_myArray', [...myArray]);
},
rollbackMyArray() {
let _myArray = this.get('_myArray ');
if(_myArray) this.set('myArray', [..._myArray ]);
},
rollbackAttributes() {
this._super(...arguments);
this.rollbackMyArray();
},
save() {
this.cacheMyArray();
return this._super(...arguments)
.then((result) => {
return result;
})
.catch((error) => {
throw error;
});
},