Skip to content
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

Open
saeid85 opened this issue Apr 6, 2019 · 7 comments
Open

Persist an Array in @observable Array #73

saeid85 opened this issue Apr 6, 2019 · 7 comments

Comments

@saeid85
Copy link

saeid85 commented Apr 6, 2019

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.

users = []
user={
   id: 1,
   name: "John",
   family: "Doe",
   orders:[],
   ...
}
order={
   id: 1,
   ...
}

I created a store like this:

import { observable, computed } from "mobx";
import { persist } from "mobx-persist";

class usersStore {
  @persist("list")
  @observable
  users = [];

  addUsers(userFirstName, userLastName) {
    this.users.push({
      id: this.generateId(),
      addedDate: new Date(),
      userFirstName,
      userLastName,
      orders: []
    });
  }

  addOrder(id, order) {
    this.users.filter(asset => asset.id === id)[0].orders.push(order);
  }
}

const usersStore = new UsersStore();
export default usersStore;

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

@doKill
Copy link

doKill commented May 16, 2019

same question

@doKill
Copy link

doKill commented May 16, 2019

@saeid85 do u resolve it?

@saeid85
Copy link
Author

saeid85 commented May 17, 2019

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.

@doKill
Copy link

doKill commented May 29, 2019

@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 }));
});

@saeid85
Copy link
Author

saeid85 commented May 31, 2019

Cool :)

@mthuong
Copy link

mthuong commented Oct 21, 2019

@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.

@mthuong
Copy link

mthuong commented Oct 21, 2019

I realized that the problem is about the mobx v4 and v5 and IObservableArray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants