Skip to content

Instantly share code, notes, and snippets.

@birkir
Created September 14, 2018 16:08
Show Gist options
  • Save birkir/0bead0d4a05577c79490201c0f11beac to your computer and use it in GitHub Desktop.
Save birkir/0bead0d4a05577c79490201c0f11beac to your computer and use it in GitHub Desktop.
export const Product = types.model('Product', {
id: types.identifier,
name: types.string,
price: types.number,
})
.actions(self => ({
update() {
},
});
export const Products = types.model('Products', {
items: types.map(Product),
})
.actions(self => ({
loadProductById: flow(function* (productId: string) {
const productData = fetch(`http://products.org/${productId}`).then(res => res.json());
const product = self.items.get(productData.id);
if (!product) {
self.items.put(productData);
} else {
applySnapshot(product, productData);
}
return self.items.get(productData.id);
}),
})
.views(self => ({
getOrLoadById(productId: string) {
const product = self.items.get(productId);
if (!product) {
setImmediate(() => self.loadProductById(productId));
}
return movie;
},
}))
.create();
@observer
export class Screen extends React.Component {
render() {
const product = Products.getOrLoadById(this.props.id);
return (
<div>Product Name: {product.name}</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment