Created
September 14, 2018 16:08
-
-
Save birkir/0bead0d4a05577c79490201c0f11beac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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