Last active
May 12, 2016 06:39
-
-
Save fpauser/1fa7ebcc0447307c8ea48c9b6aaa5771 to your computer and use it in GitHub Desktop.
Countdown with Ember
This file contains hidden or 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
import Ember from 'ember'; | |
const minSecs = 60; | |
const hourSecs = 60 * minSecs; | |
const daySecs = 24 * hourSecs; | |
export default Ember.Component.extend({ | |
classNames: 'count-down', | |
timeout: null, | |
delay: null, | |
days: 0, | |
hours: 0, | |
minutes: 0, | |
seconds: 0, | |
isDays: Ember.computed.gt('days', 0), | |
isHours: Ember.computed.gt('hours', 0), | |
isMinutes: Ember.computed.gt('minutes', 0), | |
showDays: Ember.computed.alias('isDays'), | |
showHours: Ember.computed.or('isDays', 'isHours'), | |
showMinutes: Ember.computed.or('showHours', 'isMinutes'), | |
multipleDays: Ember.computed.gt('days', 1), | |
multipleHours: Ember.computed.gt('hours', 1), | |
multipleMinutes: Ember.computed.gt('minutes', 1), | |
multipleSeconds: Ember.computed.gt('seconds', 1), | |
_currentTimeout: null, | |
didInsertElement() { | |
this._super(...arguments); | |
Ember.run.next(this, () => { | |
this.set('remaining', this.get('timeout')); | |
this.refresh(); | |
}); | |
}, | |
willClearRender() { | |
this._super(...arguments); | |
let _currentTimeout = this.get('_currentTimeout'); | |
if (_currentTimeout) { Ember.run.cancel(currentTimeout); } | |
}, | |
refresh() { | |
let remaining = this.get('remaining'); | |
let delay = this.get('delay') || 1000; | |
let step = this.get('step') || 1; | |
let d = Math.floor(remaining / daySecs); | |
let h = Math.floor((remaining - (d * daySecs)) / hourSecs); | |
let m = Math.floor((remaining - (d * daySecs) - (h * hourSecs)) / minSecs); | |
let s = Math.floor(remaining - (d * daySecs) - (h * hourSecs) - (m * minSecs)); | |
remaining -= step; | |
if (remaining <= 0) { | |
remaining = d = h = m = s = 0; | |
} | |
this.setProperties({ | |
remaining, | |
days: d, | |
hours: h, | |
minutes: m, | |
seconds: s | |
}); | |
if (remaining > 0) { | |
let _currentTimeout = Ember.run.later(this, this.refresh, delay); | |
this.set('currentTimeout', _currentTimeout); | |
} | |
} | |
}); |
This file contains hidden or 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
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains hidden or 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.count-down { | |
margin-bottom: 10px; | |
display: inline-block; | |
border: 2px solid #555; | |
padding: 5px 10px; | |
} | |
.count-down > .top-row { | |
font-size:11px; | |
color:#555 | |
} | |
.count-down > .bottom-row { | |
margin-top: 5px; | |
} |
This file contains hidden or 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
{ | |
"version": "0.8.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.5.1", | |
"ember-data": "2.5.2", | |
"ember-template-compiler": "2.5.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running app:
https://ember-twiddle.com/1fa7ebcc0447307c8ea48c9b6aaa5771?openFiles=templates.components.count-down.hbs%2Ctemplates.components.count-down.hbs