mobx-keystone-asyncstore
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -160,2 +160,3 @@ import { AnyModel, ModelClass, ModelProps } from "mobx-keystone"; | ||
containers: Map<string, T>; | ||
values: T[]; | ||
isReady: boolean; | ||
@@ -162,0 +163,0 @@ isPending: boolean; |
{ | ||
"name": "mobx-keystone-asyncstore", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"author": "Marcus Ekwall <marcus.ekwall@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "An opinionated asynchronous store and container implementation for mobx-keystone.", |
@@ -23,3 +23,3 @@ # mobx-keystone-asyncstore | ||
import { when } from "mobx"; | ||
import { model, modelAction, Model, tProps } from "mobx-keystone"; | ||
import { model, modelAction, Model, tProp, types } from "mobx-keystone"; | ||
import { AsyncStore } from "mobx-keystone-asyncstore"; | ||
@@ -30,6 +30,6 @@ | ||
class TodoItem extends Model({ | ||
id: tProps(types.string), | ||
task: tProps(types.string), | ||
done: tProps(types.boolean, false) | ||
}){ | ||
id: tProp(types.string), | ||
task: tProp(types.string), | ||
done: tProp(types.boolean, false), | ||
}) { | ||
@modelAction | ||
@@ -39,3 +39,3 @@ public toggleDone() { | ||
} | ||
}; | ||
} | ||
@@ -45,23 +45,27 @@ // Create async store | ||
@model(storeName) | ||
class TodoStore extends AsyncStore(TodoItem, { | ||
{ name: storeName }, | ||
// Logic to fetch one item | ||
async fetchOne(id: string) { | ||
const res = await axios.get(`/todos/${id}`); | ||
return new TodoItem(res.data); | ||
class TodoStore extends AsyncStore( | ||
TodoItem, | ||
{ | ||
name: storeName, | ||
// Logic to fetch one item | ||
async fetchOne(id: string) { | ||
const res = await axios.get(`/todos/${id}`); | ||
return new TodoItem(res.data); | ||
}, | ||
// Logic to fetch many items | ||
async fetchMany(ids: string[]) { | ||
const res = await axios.get(`/todos`, { ids }); | ||
return res.data.response.map((d: any) => new TodoItem(d)); | ||
}, | ||
// Logic to fetch all items | ||
async fetchAll() { | ||
const res = await axios.get(`/todos/all`); | ||
return res.data.response.map((d: any) => new TodoItem(d)); | ||
}, | ||
}, | ||
// Logic to fetch many items | ||
async fetchMany(ids: string[]) { | ||
const res = await axios.get(`/todos`, { ids }); | ||
return res.data.response.map((d) => new TodoItem(d)); | ||
}, | ||
// Logic to fetch all items | ||
async fetchAll() { | ||
const res = await axios.get(`/todos/all`); | ||
return res.data.response.map((d) => new TodoItem(d)); | ||
}, | ||
}, { | ||
// Add additional model props for the store | ||
isDirty: tProp(types.boolean, true) | ||
}) { | ||
{ | ||
// Add additional model props for the store | ||
isDirty: tProp(types.boolean, true), | ||
} | ||
) { | ||
@modelAction | ||
@@ -80,3 +84,3 @@ public setDirty(isDirty = true) { | ||
// Add container to store | ||
this.containers.set(task.id, task); | ||
this.containers.set(task.id, container); | ||
// Set the store as dirty | ||
@@ -89,3 +93,3 @@ this.setDirty(); | ||
// Method to save all our todos | ||
async saveToDataBase() { | ||
async saveToDb() { | ||
if (this.isDirty) { | ||
@@ -104,3 +108,3 @@ const res = await axios.post(`/todos`, this.values); | ||
// Ask the store to return container with id 'foo' | ||
const container = todoStore.get("foo"); | ||
const container = todoStore.getOne("foo"); | ||
@@ -121,3 +125,3 @@ // Wait for the container to be ready to be consumed | ||
id: "bar", | ||
task: "Do this thing as well" | ||
task: "Do this thing as well", | ||
}) | ||
@@ -124,0 +128,0 @@ ); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45209
609
125