Created
June 25, 2014 00:36
-
-
Save givanse/6ce9a8d2bd13e5cb73d2 to your computer and use it in GitHub Desktop.
Single route app. http://emberjs.jsbin.com/socum/1/edit?html,js,output
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
/* Put your CSS here */ | |
html, body { | |
margin: 20px; | |
} | |
li { | |
border: 1px solid #e0e0e0; | |
border-radius: 4px; | |
margin: 1px; | |
padding: 2px; | |
background-color: #e0e0e0; | |
list-style-type: none; | |
cursor: pointer; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Single route App" /> | |
<meta charset="utf-8"> | |
<title>Ember Starter Kit</title> | |
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css"> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script> | |
<script src="http://builds.emberjs.com/tags/v1.5.1/ember.js"></script> | |
</head> | |
<body> | |
<script type="text/x-handlebars"> | |
<h2>Office Supplies</h2> | |
{{outlet "modal"}} | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
{{outlet "sale"}} | |
{{outlet "items"}} | |
{{outlet "cart"}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="items"> | |
<h4>Items</h4> | |
<ul> | |
{{#each controller}} | |
<li {{action "addItem" this}}> | |
${{formattedPrice}} {{name}} | |
</li> | |
{{/each}} | |
</ul> | |
</script> | |
<script type="text/x-handlebars" data-template-name="cart"> | |
<h4>Cart</h4> | |
<ul> | |
{{#each selectedItems itemController="item"}} | |
<li {{action "removeItem" this}}> | |
${{formattedPrice}} {{name}} | |
</li> | |
{{/each}} | |
</ul> | |
</script> | |
<script type="text/x-handlebars" data-template-name="sale"> | |
<h4>Sale</h4> | |
Sub-total: ${{formattedSubtotal}}<br> | |
Taxes: {{tax}}%<br> | |
Total: ${{total}} | |
<br><br> | |
<button {{action "checkOut"}}> | |
Checkout | |
</button> | |
</script> | |
<script type="text/x-handlebars" data-template-name="modal"> | |
<div> | |
Proceed with checkout? | |
<button {{action "cancel"}}> | |
Cancel | |
</button> | |
</div> | |
</script> | |
</body> | |
</html> |
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
App = Ember.Application.create(); | |
App.Router.map(function() { | |
/** Single route (IndexRoute) **/ | |
}); | |
App.IndexRoute = Ember.Route.extend({ | |
setupController: function(controller, model) { | |
this._super(controller, model); | |
this.controllerFor('items') | |
.set('model', fixture.items); | |
}, | |
renderTemplate: function(controller, model) { | |
this._super(controller, model); | |
this.renderInIndex('items'); | |
this.renderInIndex('cart'); | |
this.renderInIndex('sale'); | |
}, | |
renderInIndex: function(name) { | |
this.render(name, { | |
outlet: name, | |
into: 'index', | |
controller: name | |
}); | |
}, | |
actions: { | |
addItem: function(item) { | |
console.log('DashboardRoute.addItem: ' + item); | |
var c = this.controllerFor("cart"); | |
var propertyName = "selectedItems"; | |
c.get(propertyName).pushObject(item); | |
c = this.controllerFor("sale"); | |
c.incrementProperty("subtotal", item.price); | |
}, | |
removeItem: function(item) { | |
console.log('DashboardRoute.removeItem: ' + item); | |
/* not implemented */ | |
}, | |
checkOut: function() { | |
console.log("checkout"); | |
this.render('modal', { | |
into: 'application', | |
outlet: 'modal' | |
}); | |
}, | |
cancel: function() { | |
this.disconnectOutlet({ | |
outlet: 'modal', parentView: 'application'}); | |
}, | |
proceed: function() { | |
} | |
} | |
}); | |
App.ItemsController = Ember.ArrayController.extend({ | |
itemController: 'item' | |
}); | |
App.ItemController = Ember.ObjectController.extend({ | |
formattedPrice: function() { | |
var price = this.get('price'); | |
return price.toFixed(2); | |
}.property('price') | |
}); | |
App.CartController = Ember.Controller.extend({ | |
selectedItems: [] | |
}); | |
App.SaleController = Ember.Controller.extend({ | |
tax: 12, | |
total: function() { | |
var st = this.get('subtotal'); | |
var tax = this.get('tax'); | |
var total = st + (tax * st / 100); | |
return total.toFixed(2); | |
}.property('subtotal', 'tax'), | |
subtotal: 0.0, | |
formattedSubtotal: function() { | |
return this.get('subtotal').toFixed(2); | |
}.property('subtotal') | |
}); | |
var fixture = { | |
items: [ | |
{name: 'paperclips', price: 0.5}, | |
{name: 'staples', price: 0.251}, | |
{name: 'paper', price: 0.75}] | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment