Store
A store holds the whole state tree of your application.
To create a store, use createResourceStore
.
Store Methods
Store Methods
getResources(resourceType, [filter], [options])
getResources(resourceType, [filter], [options])
Retrieve resources from the state tree.
Arguments
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 everyresource of this type will be returned.
[
options
] (Object): An object to customize the response. Presently, there is only one option:byId
. Passtrue
to receive the resources as an Object keyed by their IDs rather than as an array.
Returns
(Array|Object): The resources that match the filter.
Example
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:
To learn more about getResources
, refer to the guide on Retrieving Resources.
getGroup(groupName, [options])
getGroup(groupName, [options])
Retrieve a group from the state tree.
Arguments
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
. Passtrue
to receive the resources as an Object keyed by ID rather than as an array.
Returns
(Array|Object): The resources contained in the group. If the group does not exist, then an empty array or object will be returned.
Example
To learn more about getGroup
, refer to the guide on Retrieving Groups.
update([path], [changes], [options])
update([path], [changes], [options])
Create or update resources and groups within the state tree.
Arguments
[
path
] (string): An optional string that allows you to specify a path in the state treeyou 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 wishto update.
[
options
] (Object): Configure the update. There are two options. The first ismergeResources
, whichdetermines whether or not existing resource data is merged with what you pass in. The default is
true
. Theother option is
concatGroups
, which isfalse
by default. Pass this astrue
, and existing groups will beconcatenated with the groups that you pass in.
Returns
None.
Example
To learn more about store.update()
, refer to the following guides:
remove([path], changes)
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
Arguments
[
path
] (string): An optional string that allows you to specify a path in the state treeyou 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 wishto delete. Anything that is set to
null
withinchanges
will be deleted from the state tree.
Returns
None.
Example
To learn more about the remove()
method, refer to these guides:
getState()
getState()
Returns the entire state tree.
Returns
(Object): The current state tree of your application.
Notes
You should use
getResources
andgetGroup
instead ofgetState
within your application.They are typically more convenient to use than
getState
.getState
should beused for serializing your store in a universal app, or for persisting user state between
sessions.
Example
subscribe(listener)
subscribe(listener)
Subscribe to changes to the store.
Arguments
listener
(Function). A function that will be called anytime thatstore.update()
or
store.remove()
is called. The listener will be called whether or not the storewas changed.
Returns
(Function): A function that unsubscribes the listener when called.
Example
Last updated