Last active
February 26, 2016 18:04
-
-
Save mweststrate/5c1a29ee80832f28f3be to your computer and use it in GitHub Desktop.
data model using mobservables
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
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