<html>
<head>
<meta charset=utf-8 />
<title>Cookbook: Continuous Redrawing of Views</title>
</head>
<body>
<script type="text/x-handlebars">
{{outlet}}
</script>
<script type="text/x-handlebars" id="interval">
{{#if fullSecond}}
{{nyan-start}}
{{/if}}
{{#if quarterSecond}}
{{nyan-middle}}
{{/if}}
{{#if halfSecond}}
{{nyan-end}}
{{/if}}
{{#if threeQuarterSecond}}
{{nyan-middle}}
{{/if}}
<h3>You've nyaned for {{digital_clock clock.pulse}} (h:m:s)</h3>
{{render comments}}
</script>
<script type="text/x-handlebars" id="components/nyan-start">
<pre>{{cat}}</pre>
</script>
<script type="text/x-handlebars" id="components/nyan-middle">
<pre>{{cat}}</pre>
</script>
<script type="text/x-handlebars" id="components/nyan-end">
<pre>{{cat}}</pre>
</script>
<script type="text/x-handlebars" id="comments">
<input type="text" id="comment" />
<button {{action add}}>Add Comment</button>
<ul>
{{#each}}
<li>{{comment}} ({{digital_clock seconds}})</li>
{{/each}}
</ul>
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.1.2/handlebars.js"></script>
<script src="http://builds.emberjs.com/tags/v1.2.0/ember.js"></script>
</body>
</html>
(function (host) {
var ClockService = Ember.Object.extend({
pulse: Ember.computed.oneWay('_seconds').readOnly(),
tick: function () {
var clock = this;
Ember.run.later(function () {
var seconds = clock.get('_seconds');
if (typeof seconds === 'number') {
clock.set('_seconds', seconds + (1/4));
}
}, 250);
}.observes('_seconds').on('init'),
_seconds: 0
});
Ember.Application.initializer({
name: 'clockServiceInitializer',
initialize: function(container, application) {
container.register('clock:service', ClockService);
application.inject('controller:interval', 'clock', 'clock:service');
}
});
Ember.Handlebars.registerBoundHelper('digital_clock', function(seconds) {
var h = Math.floor(seconds / 3600);
var m = Math.floor((seconds % 3600) / 60);
var s = Math.floor(seconds % 60);
var addZero = function (number) {
return (number < 10) ? '0' + number : '' + number;
};
var formatHMS = function(h, m, s) {
if (h > 0) {
return '%@:%@:%@'.fmt(h, addZero(m), addZero(s));
}
return '%@:%@'.fmt(m, addZero(s));
};
return new Ember.Handlebars.SafeString(formatHMS(h, m, s));
});
var App = Ember.Application.create();
App.Router.map(function () {
this.route('interval');
});
App.IndexRoute = Ember.Route.extend({
redirect: function () {
this.transitionTo('interval');
}
});
App.IntervalController = Ember.ObjectController.extend({
secondsBinding: 'clock.pulse',
fullSecond: function () {
return (this.get('seconds') % 1 === 0);
}.property('seconds'),
quarterSecond: function () {
return (this.get('seconds') % 1 === 1/4);
}.property('seconds'),
halfSecond: function () {
return (this.get('seconds') % 1 === 1/2);
}.property('seconds'),
threeQuarterSecond: function () {
return (this.get('seconds') % 1 === 3/4);
}.property('seconds')
});
App.CommentItemController = Ember.ObjectController.extend({
seconds: Ember.computed.oneWay('clock.pulse').readOnly()
});
App.CommentsController = Ember.ArrayController.extend({
needs: ['interval'],
itemController: 'commentItem',
actions: {
add: function () {
this.addObject(Em.Object.create({
comment: $('#comment').val(),
clock: ClockService.create()
}));
}
}
});
App.NyanStartComponent = Ember.Component.extend({
cat: function () {
return [
' + o + o',
' + o + +',
' o +',
'-------_,------, + o',
'-------_| /\\_/\\',
'-------^|__( - .-) o + +',
'------- "" ""',
' + o o + o',
' + +',
' o o o o +'
].join('\n');
}.property()
});
App.NyanMiddleComponent = Ember.Component.extend({
cat: function () {
return [
' + o + o',
' + o + +',
' o +',
'________-------_,------, o',
'________-------_| /\\_/\\',
'________-------^|__( - .-) + +',
'________------- "" ""',
' + o o + o',
' + +',
' o o o o +'
].join('\n');
}.property()
});
App.NyanEndComponent = Ember.Component.extend({
cat: function () {
return [
' + o + o',
' + o + +',
' o +',
'-------________-------_,------, o',
'-------________-------_| /\\_/\\',
'-------________-------^|__( - .-) +',
'-------________------- "" ""',
' + o o + o',
' + +',
' o o o o +'
].join('\n');
}.property()
});
host.App = App;
}(window));
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. |