diff --git a/addon/components/datepicker-support.js b/addon/components/datepicker-support.js index 5b868bd..5538418 100644 --- a/addon/components/datepicker-support.js +++ b/addon/components/datepicker-support.js @@ -9,6 +9,9 @@ export default Ember.Mixin.create({ language: undefined, startDate: undefined, endDate: undefined, + customParser: function(value) { + return value; + }, setupBootstrapDatepicker: Ember.on('didInsertElement', function() { @@ -134,6 +137,7 @@ export default Ember.Mixin.create({ _updateDatepicker: function() { var element = this.$(), value = this.get('value'), + customParser = this.get('customParser'), dates = []; if (!this.get('mustUpdateInput')) { @@ -141,6 +145,8 @@ export default Ember.Mixin.create({ return; } + value = customParser(value); + switch (Ember.typeOf(value)) { case 'array': dates = value; diff --git a/tests/unit/components/bootstrap-datepicker-test.js b/tests/unit/components/bootstrap-datepicker-test.js index dcae6ac..8c27b3f 100644 --- a/tests/unit/components/bootstrap-datepicker-test.js +++ b/tests/unit/components/bootstrap-datepicker-test.js @@ -42,6 +42,32 @@ test('displays date with custom format when format is set', function(assert) { assert.equal(this.$().val(), '31.Dec.14'); }); +test('resets date when input is cleared', function(assert) { + this.subject({ + value: new Date(2014, 11, 31) + }); + + assert.ok(this.$().datepicker('getDate'), 'initial value is set'); + + this.$().val(''); + this.$().trigger('input'); + + assert.equal(this.$().datepicker('getDate'), null, 'value is reset when input is cleared'); +}); + +test('should use customParser if provided', function(assert) { + assert.expect(1); + + this.subject({ + value: '2015-09-14T16:59:01+02:00', + customParser: function(value) { + return new Date(value); + } + }); + + assert.equal(this.$().val(), '09/14/2015'); +}); + test('sets dates provided by value (multidate, default multidateSeparator)', function(assert) { this.subject({ value: [new Date(2015, 0, 13), new Date(2015, 0, 7), new Date(2015, 0, 15)],