A simple clock service for Ember.js
View a live demo here: http://clock.jerel.co/
ember install ember-cli-clock
-
Generate a clock service
ember generate clock my-shiny-new-clock
-
Inject the
my-shiny-new-clock
service into your component, controller, route, ...clock: service('my-shiny-new-clock')
-
Use the
time
(Date.now()
) anddate
(Date
instance) properties of the clock service
// app/components/iso-date.js
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
export default Component.extend({
clock: service('my-shiny-new-clock'),
iso: computed('clock.date', function() {
return this.get('clock.date').toISOString();
})
});
Using {{iso}}
in the template will output something like
2011-10-05T14:48:00.000Z
and update it every second.
// app/components/device-status.js
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';
export default Component.extend({
clock: service('my-shiny-new-clock'),
isOnline: computed('lastContact', 'clock.time', function() {
return this.get('clock.time') - this.get('lastContact') < 60 * 1000;
})
});
The isOnline
property is updated every second and will tell
if the last contact of the device was less than 60 seconds ago.
interval
(default: 1000ms) - the update interval oftime
anddate
time
(read-only) - will be set toDate.now()
everyinterval
millisecondsdate
(read-only) - computed property:new Date(this.get('time'))
start()
- starts the clock after it has been stoppedstop()
- stops the clock from updatingtime
anddate
until restarted
Copyright (c) 2014 Jerel Unruh and contributors