Skip to content

Instantly share code, notes, and snippets.

@cableray
Forked from bpalij/store.js
Last active January 28, 2021 17:35
Show Gist options
  • Save cableray/56b7b1cccbcac66cb2076a47feffb69c to your computer and use it in GitHub Desktop.
Save cableray/56b7b1cccbcac66cb2076a47feffb69c to your computer and use it in GitHub Desktop.
const defaultSnapshot = {
token: '',
myInnerInfo: { login: '', type: '' },
myDisplayInfo: { login: '', type: '' },
loginInfo: { login: '', type: '' },
loginList: [],
loading: false,
logined: false,
}
const User = types
.model({
login: '',
type: '',
}).actions(self => ({
setUserInfo({ login, type }) {
self.login = login;
self.type = type;
}
}))
const RootStore = types
.model({
token: '',
// If these are not optional, then you must specify User data when creating model
// Since User has all optional values itself, setting it as `optional` allows it to be created automatically
myInnerInfo: types.optional(User, {}),
myDisplayInfo: types.optional(User, {}),
loginInfo: types.optional(User, {}),
loginList: types.array(types.string),
loading: false,
logined: false,
})
// with the optionals, you can create a model like this:
const createdWithNoData = RootStore.create()
// without the optionals you Must provide the data for the non-optional properties, or it will cause an error
const createdWithData = RootStore.create({
myInnerInfo: { login: '', type: '' },
myDisplayInfo: { login: '', type: '' },
loginInfo: { login: '', type: '' }
})
// In both cases, the data will be the same (i.e. there will be User objects created for myInnerInfo and friends)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment