pinqy520 / mobx-persist

persist mobx stores
MIT License
560 stars 62 forks source link

Array not persist in @observable object #57

Open ChandreshRana opened 6 years ago

ChandreshRana commented 6 years ago
@persist('object') @observable dataObj = {
    employees: [],
    equipments: [],
  }

I have above persist observable object and its properties employees and equipments. When I push data in employees (eg. employess = [ {}, {} ] )

Now when I reopen app, I can not get employees with data, I got only blank array.

console.log('Employees: ', store.dataObj.employees)

print => []

PhilTheAir commented 5 years ago

same here....

erzzo commented 5 years ago

same!

rommyarb commented 5 years ago

You didn't read the documentation my brother.

Do it like this:

class DataObj {
  @persist('list') @observable employees = [];
  @persist('list') @observable equipments = [];
}

class MyStore {
  @persist('object', DataObj) @observable dataObj = new DataObj();
}
erzzo commented 5 years ago

We have a problem with nested, not plain arrays. { someitem: [ ] }. Like he showed in the example

rommyarb commented 5 years ago

We have a problem with nested, not plain arrays. { someitem: [ ] }. Like he showed in the example

LOL. I just showed you nested example. Take a look of my reply above.

rommyarb commented 5 years ago

@erzzo Okay. If you still don't understand. Let me explain:

Instead of doing this:

class MyStore {
  @persist('object') @observable dataObj = {
    employees: [],
    equipments: []
  }
}

...

export const myStore = new MyStore();

do this:

class DataObj {
  @persist('list') @observable employees = [];
  @persist('list') @observable equipments = [];
}
class MyStore {
  @persist('object', DataObj) @observable dataObj = new DataObj();
}

...

export const myStore = new MyStore();

Do you still not understand?

erzzo commented 5 years ago

Sorry, it's the morning :D looks good. Will try it. So just need to create an observable array then use it in the object. :+1: