I've got an app using mobx-state-tree
that currently has a few simple stores:
Article
represents an article, either sourced through a 3rd party API or written in-houseArticleStore
holds references to articles: { articles: {}, isLoading: bool }
This setup works well for simple use-cases, such as fetching articles based on ID. E.g.
/article/{articleUri}
articleStoreInstance.fetch([articleUri])
returns the article in questionarticleStoreInstance.articles.get(articleUri)
For a more complex scenario, if I wanted to fetch a set of articles based on a complex query, e.g. { offset: 100, limit: 100, freeTextQuery: 'Trump' }
, should I then:
SearchResult
store that simply links to the articles that the user has searched forSearchResult
store that I pass around for as long as I need it?I should add that I'd like to keep articles in the stores between page-loads to avoid re-fetching the same content over and over.
Is there a somewhat standardized way of addressing this problem? Any examples to look at?
What you need might be a Search
store which keeps track of following information:
Then to avoid storing articles in 2 places, the query results should not use Article
model, but reference to Article
model. Anytime you query, the actual result will be saved in existing store ArticleStore
, and Search
only holds references:
import { types, getParent, flow } from 'mobx-state-tree'
const Search = types.model({
params: // your own params info
results: types.array(types.reference(Article))
}).views(self => ({
get parent() {
return getParent(self) // get root node to visit ArticleStore
}
})).actions(self => ({
search: flow(function*(params) {
this.params = params // save query params
const result = yield searchByQuery(query) // your query here
this.parent.articleStore.saveArticles(result) // save result to ArticleStore
this.results = getArticleIds(result) // extract ids here for references
})
}))
Hope it's what you are looking for.