<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" id="index">
<ul>
{{#each item in model}}
<li>{{item}}</li>
{{/each}}
</ul>
<table>
<h2>Total: {{totalBoth}}</h2>
<thead>
<tr>
<th>name</th>
<th>count A</th>
<th></th>
<th>count B</th>
<th></th>
<th>total</th>
</tr>
</thead>
<tbody>
{{#each type in App.TypeNumbers}}
<tr>
<td>{{type.name}}</td>
<td>
<button {{action "typeADecrease" type}}>-</button>
{{input value=type.countA}}
<button {{action "typeAIncrease" type}}>+</button>
</td>
<td>{{type.typeA}}</td>
<td>
<button {{action "typeBDecrease" type}}>-</button>
{{input value=type.countB}}
<button {{action "typeBIncrease" type}}>+</button>
</td>
<td>{{type.typeB}}</td>
<td>{{type.calculatedTotal}}</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
<script src="http://builds.emberjs.com/ember-latest.js"></script>
<script src="http://builds.emberjs.com/ember-data-latest.js"></script>
<script type="text/javascript">
App = Ember.Application.create();
App.Router.map(function() {
// put your routes here
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return ['red', 'yellow', 'blue'];
}
});
App.IndexController = Ember.ArrayController.extend({
totalBoth : function(){
var total = 0;
App.TypeNumbers.forEach(function(item){
console.log( item.get('calculatedTotal') );
total += item.get('calculatedTotal');
});
return total;
}.property( 'App.TypeNumbers.@each.calculatedTotal' ),
actions : {
typeADecrease : function(type){
var currentCount = type.get('countA');
if (currentCount <= 0){
type.set('countA', 0);
}else{
type.set('countA', --currentCount);
}
},
typeAIncrease : function(type){
var currentCount = type.get('countA');
type.set('countA', ++currentCount);
},
typeBDecrease : function(type){
var currentCount = type.get('countB');
if (currentCount <= 0){
type.set('countB', 0);
}else{
type.set('countB', --currentCount);
}
},
typeBIncrease : function(type){
var currentCount = type.get('countB');
type.set('countB', ++currentCount);
},
}
});
App.TypeNumber= Ember.Object.extend({
name : '',
typeA : 11,
countA : 0,
typeB : 22,
countB : 0,
calculatedA : function(){
return ( this.get('typeA') * this.get('countA') );
}.property('typeA', 'countA'),
calculatedB : function(){
return ( this.get('typeB') * this.get('countB') );
}.property('typeB', 'countB'),
calculatedTotal : function(){
return ( this.get('calculatedA') + this.get('calculatedB') );
}.property( 'calculatedA', 'calculatedB' ),
});
App.TypeNumbers = [
App.TypeNumber.create({
name : 'test 1',
typeA : 1,
countA : 2,
typeB : 2,
countB : 0,
}),
App.TypeNumber.create({
name : 'test 2',
typeA : 11,
countA : 0,
typeB : 22,
countB : 0,
}),
App.TypeNumber.create({
name : 'test 3',
typeA : 111,
countA : 0,
typeB : 222,
countB : 0,
}),
App.TypeNumber.create({
name : 'test 4',
typeA : 1111,
countA : 0,
typeB : 2222,
countB : 0,
})
];
</script>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |