-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Persist an Array in @observable Array #73
Comments
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 Storage.get("offline").then(v => {
Storage.update("offline",Object.assign(v, { projectList: this.projectList }));
}); |
Cool :) |
Why do you need to like this? user.orders[user.orders.length-1] = { 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
The text was updated successfully, but these errors were encountered: