Skip to content

Instantly share code, notes, and snippets.

@tavlima
Forked from locks/application.controller.js
Created October 16, 2016 00:10
Show Gist options
  • Save tavlima/322c21253b49d9434c649a61112f2271 to your computer and use it in GitHub Desktop.
Save tavlima/322c21253b49d9434c649a61112f2271 to your computer and use it in GitHub Desktop.
Tick
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Ember Twiddle',
timers: null,
// contrived examples are contrived, I used `init` because I know what I'm doing
initTimers: Ember.on('init', function() {
this.set('timers', [
Ember.Object.create({ time: 0, ticking: true}),
Ember.Object.create({ time: 0, ticking: true})]);
this.tick();
}),
tick: function() {
this.get('timers').forEach(timer => {
if (timer.get('ticking')) {
timer.set('time', timer.get('time') + 1);
}
});
Ember.run.later(this, this.tick, 1000);
},
actions: {
stopTimer(index) {
this.get('timers').objectAt(index).set('ticking', false);
},
startTimer(index) {
this.get('timers').objectAt(index).set('ticking', true);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{#each timers as |timer index|}}
{{timer-component timer=timer stop=(action 'stopTimer' index) start=(action 'startTimer' index)}}
{{/each}}
<br>
<br>
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
stop() {
this.get('stop')();
},
start() {
this.get('start')();
}
}
});
<p>{{timer.time}}</p>
<button {{action 'stop'}}>Stop</button>
<button {{action 'start'}}>Start</button>
{
"version": "0.4.14",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment