var autoNumber = 0;
/**
* Data model
*/
function Article(name, price) {
this.id = ++autoNumber; // UUID for this article
// Mobservable.props creates observable values and property accessors to read / write them
mobservable.props(this, {
name: name,
price: price
});
}
function ShoppingCartEntry(article) {
this.id = ++autoNumber; // UUID for this entry
mobservable.props(this, {
article: article,
amount: 1,
price: function() {
return this.article ? this.article.price * this.amount : 0;
}
});
}
function ShoppingCart() {
mobservable.props(this, {
entries: [],
total: function() {
return this.entries.reduce(function(sum, entry) {
return sum + entry.price;
}, 0);
}
});
}
// Some available articles
var articles = mobservable.array([
["Funny Bunnies", 17.63],
["Awesome React", 23.95],
["Second hand Netbook", 50.00]
].map(function(e) {
return new Article(e[0], e[1]);
}));
// Our shopping cart
var shoppingCart = new ShoppingCart();
// With a demo item inside
shoppingCart.entries.push(new ShoppingCartEntry(articles[0]));
/**
* UI logic
*/
var ShopDemoView = React.createClass({
mixins: [mobservable.ObserverMixin, React.addons.PureRenderMixin],
render: function() {
// all render methods in a component with the ObserverMixin will automatically
// subscribe to any observable values used in the render function
// (and clean up...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.