Store
Last updated
Last updated
A store holds the whole state tree of your application.
To create a store, use .
getResources(resourceType, [filter], [options])
Retrieve resources from the state tree.
resourceType
(string): The type of resource to be retrieved.
[filter
] (Array|Object|function): A filter to apply to the resources. If it is omitted, then every
resource of this type will be returned.
[options
] (Object): An object to customize the response. Presently, there is only one option:
byId
. Pass true
to receive the resources as an Object keyed by their IDs rather than as an array.
(Array|Object): The resources that match the filter.
In this example, we return all of the books in the store.
Return every book published in 1970:
Return every book without a publish year:
getGroup(groupName, [options])
Retrieve a group from the state tree.
groupName
(string): The name of the group to be retrieved.
[options
] (Object): An object to customize the response. Presently, there is only one option:
byId
. Pass true
to receive the resources as an Object keyed by ID rather than as an array.
(Array|Object): The resources contained in the group. If the group does not exist, then an empty array or object will be returned.
update([path], [changes], [options])
Create or update resources and groups within the state tree.
[path
] (string): An optional string that allows you to specify a path in the state tree
you are updating. Passing this can allow you to type less code, as it allows you to "drill" into
the state tree to update a specific resource or group.
[changes
] (Object): This allows you to further specify the sections of the state tree that you wish
to update.
[options
] (Object): Configure the update. There are two options. The first is mergeResources
, which
determines whether or not existing resource data is merged with what you pass in. The default is true
. The
other option is concatGroups
, which is false
by default. Pass this as true
, and existing groups will be
concatenated with the groups that you pass in.
None.
To learn more about store.update()
, refer to the following guides:
remove([path], changes)
Remove things from the store. You can remove:
One or many resources from the store entirely
One or many groups from the store entirely
Specific resources from a group
Specific pieces of data from resources
[path
] (string): An optional string that allows you to specify a path in the state tree
you are removing. Passing this can allow you to type less code, as it allows you to "drill" into
the state tree to remove a specific resource or group.
[changes
] (object): This allows you to further specify the sections of the state tree that you wish
to delete. Anything that is set to null
within changes
will be deleted from the state tree.
None.
To learn more about the remove()
method, refer to these guides:
getState()
Returns the entire state tree.
(Object): The current state tree of your application.
You should use getResources
and getGroup
instead of getState
within your application.
They are typically more convenient to use than getState
. getState
should be
used for serializing your store in a universal app, or for persisting user state between
sessions.
subscribe(listener)
Subscribe to changes to the store.
listener
(Function). A function that will be called anytime that store.update()
or store.remove()
is called. The listener will be called whether or not the store
was changed.
(Function): A function that unsubscribes the listener when called.
To learn more about getResources
, refer to the guide on .
To learn more about getGroup
, refer to the guide on .