Closed Kashuab closed 1 year ago
Added the following to base models:
__source: 'remote' | 'local'
__setSource(source: 'remote' | 'local')
get isRemote(): boolean
get isLocal(): boolean
In the RootStore
, you can provide the source
to the resolve
, create
and update
methods. Query
and Mutation
classes will automatically provide remote
as the source. They will all default to local
It can be helpful to determine if an instance is local, or was resolved from a server response. Imagine a
save
method on a model that needs to decide which mutation to dispatch:create
orupdate
?You can't just check
this.id
is it could throw a selection error. Base models should include the following getter that checks this:Perhaps we could abstract this behind a
hasSelected
method:Another issue I see is for instances created via
RootStore.create
since it will generate IDs automatically. Luckily, the generated IDs look like this:__$depot_auto_id:o9cmkz7lcr
so we could check for the prefix there:I wonder if we should discourage developers from using
RootStore.create
to instantiate local instances, and instead construct them directly (i.e.new UserModel()
instead ofRootStore.create(UserModel)
. One should callthis.store.replace(resourceFromMutation, this)
anyways, so there really isn't even a need to put local models in theRootStore
... perhaps that method should be made private. That's another discussion IMO.