Open saeid85 opened 5 years ago
same question
@saeid85 do u resolve it?
I found a solution & it works.
class Order {
@persist @observable id = 0;
@persist @observable amount = "";
@persist("object") @observable date = {};
}
class User {
@persist @observable id = 0;
@persist @observable name = "";
@persist @observable family = "";
@persist("list", Transaction) @observable orders = [];
}
class UsersStore {
@persist("list", Asset) @observable users = [];
@action addUser(name, family) {
this.assets.push(new User());
this.assets[this.assets.length-1] = {
id: (new Date).getTime(),
name,
family
orders: []
};
}
@action addOrder(id, order) {
let user = this.users.filter(user => user.id === id)[0];
user.orders.push(new Order());
user.orders[user.orders.length-1] = {
id: (new Date).getTime(),
amount,
date,
};
}
}
I hope it helps you.
@saeid85 thanks a lot , and i solve it by this way
Storage.get("offline").then(v => {
Storage.update("offline",Object.assign(v, { projectList: this.projectList }));
});
Cool :)
@saeid85
Why do you need to like this? user.orders.push(new Order());
user.orders[user.orders.length-1] = { id: (new Date).getTime(), amount, date, };
I had the same problem with a nested array in an array and it's not persisted.
I realized that the problem is about the mobx v4 and v5 and IObservableArray
There is a scenario which I confused how to do it using mobx-persist.
I have a list (Array) of users & I want to store an object per user. Inside user object I need to store another list (Array) call Orders and each Order is an Object itself.
I created a store like this:
adduser() pushes an object for a new user into the users array and addOrder() pushes an object for a new order into the orders array.
This approach works fine but the only issue is, it persists users list (with all objects in it) but it doesn't persist Orders list in user object.
What should I do in order to be able to persist orders list in user object?
Thanks