Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active February 26, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mweststrate/5c1a29ee80832f28f3be to your computer and use it in GitHub Desktop.
Save mweststrate/5c1a29ee80832f28f3be to your computer and use it in GitHub Desktop.
data model using mobservables
function Article(name, price) {
mobx.extendObservable(this, {
name: name,
price: price
});
}
function ShoppingCartEntry(article) {
mobx.extendObservable(this, {
article: article,
amount: 1,
price: function() {
return this.article ? this.article.price * this.amount : 0;
}
});
}
function ShoppingCart() {
mobx.extendObservable(this, {
entries: [],
total: function() {
return this.entries.reduce(function(sum, entry) {
return sum + entry.price;
}, 0);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment