Skip to content

Commit 1e5df18

Browse files
committed
Merge pull request soulim#61 from lazybensch/allow-costum-value-parsing
Allow costum value parsing
2 parents b810a20 + 67b7738 commit 1e5df18

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

addon/components/datepicker-support.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export default Ember.Mixin.create({
99
language: undefined,
1010
startDate: undefined,
1111
endDate: undefined,
12+
customParser: function(value) {
13+
return value;
14+
},
1215

1316
setupBootstrapDatepicker: Ember.on('didInsertElement', function() {
1417

@@ -134,13 +137,16 @@ export default Ember.Mixin.create({
134137
_updateDatepicker: function() {
135138
var element = this.$(),
136139
value = this.get('value'),
140+
customParser = this.get('customParser'),
137141
dates = [];
138142

139143
if (!this.get('mustUpdateInput')) {
140144
this.set('mustUpdateInput', true);
141145
return;
142146
}
143147

148+
value = customParser(value);
149+
144150
switch (Ember.typeOf(value)) {
145151
case 'array':
146152
dates = value;

tests/unit/components/bootstrap-datepicker-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ test('displays date with custom format when format is set', function(assert) {
4242
assert.equal(this.$().val(), '31.Dec.14');
4343
});
4444

45+
test('resets date when input is cleared', function(assert) {
46+
this.subject({
47+
value: new Date(2014, 11, 31)
48+
});
49+
50+
assert.ok(this.$().datepicker('getDate'), 'initial value is set');
51+
52+
this.$().val('');
53+
this.$().trigger('input');
54+
55+
assert.equal(this.$().datepicker('getDate'), null, 'value is reset when input is cleared');
56+
});
57+
58+
test('should use customParser if provided', function(assert) {
59+
assert.expect(1);
60+
61+
this.subject({
62+
value: '2015-09-14T16:59:01+02:00',
63+
customParser: function(value) {
64+
return new Date(value);
65+
}
66+
});
67+
68+
assert.equal(this.$().val(), '09/14/2015');
69+
});
70+
4571
test('sets dates provided by value (multidate, default multidateSeparator)', function(assert) {
4672
this.subject({
4773
value: [new Date(2015, 0, 13), new Date(2015, 0, 7), new Date(2015, 0, 15)],

0 commit comments

Comments
 (0)