Skip to content

Commit

Permalink
Merge pull request #203 from stefanpenner/moment-helper-global-locale-tz
Browse files Browse the repository at this point in the history
moment helper should use the service's locale and timeZone
  • Loading branch information
jasonmit authored Dec 2, 2016
2 parents 9e49b49 + 47f0ae9 commit e1f0838
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
7 changes: 3 additions & 4 deletions addon/helpers/-base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Ember from 'ember';

const { observer, inject, get, Helper } = Ember;
const { bind:runBind } = Ember.run;
const { observer, inject, get, Helper, run } = Ember;

export default Helper.extend({
moment: inject.service(),
Expand All @@ -17,7 +16,7 @@ export default Helper.extend({
this.clearTimer();

if (interval) {
this.intervalTimer = setTimeout(runBind(this, this.recompute), parseInt(interval, 10));
this.intervalTimer = run.later(this, 'recompute', parseInt(interval, 10));
}
},

Expand All @@ -39,7 +38,7 @@ export default Helper.extend({
},

clearTimer() {
clearTimeout(this.intervalTimer);
run.cancel(this.intervalTimer);
},

destroy() {
Expand Down
10 changes: 6 additions & 4 deletions addon/helpers/moment.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Ember from 'ember';
import moment from 'moment';
import BaseHelper from './-base';

export default Ember.Helper.extend({
compute(params) {
return moment(...params);
export default BaseHelper.extend({
compute(params, { locale, timeZone }) {
this._super(...arguments);

return this.morphMoment(moment(...params), { locale, timeZone });
}
});
3 changes: 3 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"dependencies": {
"ember": "~2.8.0",
"ember-cli-shims": "0.1.1"
},
"resolutions": {
"ember": "~2.8.0"
}
}
19 changes: 17 additions & 2 deletions tests/integration/moment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { moduleForComponent, test } from 'ember-qunit';
moduleForComponent('moment', {
integration: true,
beforeEach() {
moment.locale('en');
this.momentService = this.container.lookup('service:moment');
this.momentService.changeLocale('en');
}
});

Expand All @@ -27,7 +28,7 @@ test('moment-from and moment integration', function(assert) {
assert.equal(this.$().text(), 'in a day');
});

test('moment and monent-format helper integration', function(assert) {
test('moment and monent-format helper integration #2', function(assert) {
assert.expect(1);

this.setProperties({
Expand All @@ -39,3 +40,17 @@ test('moment and monent-format helper integration', function(assert) {
this.render(hbs`{{moment-format (moment date inputFormat) outputFormat}}`);
assert.equal(this.$().text(), 'May 3, 2010');
});

test('moment can use the global locale', function(assert) {
assert.expect(1);

this.setProperties({
inputFormat: 'M/D/YY',
outputFormat: 'MMMM D, YYYY',
date: '5/3/10'
});

this.momentService.changeLocale('fr');
this.render(hbs`{{moment-format (moment date inputFormat) outputFormat}}`);
assert.equal(this.$().text(), 'mai 3, 2010');
});

0 comments on commit e1f0838

Please sign in to comment.