diff --git a/ampersand-state.js b/ampersand-state.js index 79a742e..5f587de 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -587,7 +587,9 @@ var dataTypes = { date: { set: function (newVal) { var newType; - if (!_.isDate(newVal)) { + if (newVal == null) { + newType = typeof null; + } else if (!_.isDate(newVal)) { try { var dateVal = new Date(newVal).valueOf(); if (isNaN(dateVal)) { @@ -604,12 +606,14 @@ var dataTypes = { newType = 'date'; newVal = newVal.valueOf(); } + return { val: newVal, type: newType }; }, get: function (val) { + if (val == null) { return val; } return new Date(val); }, default: function () { diff --git a/test/full.js b/test/full.js index e6eaab2..52c6dd0 100644 --- a/test/full.js +++ b/test/full.js @@ -1491,6 +1491,38 @@ test("#99 #101 - string dates can be parsed", function(t) { t.end(); }); +test('#128 don\'t coerce null date as 0', function (t) { + var Day = State.extend({ + props: { + theDate: 'date' + } + }); + + var day = new Day({ theDate: null }); + t.notOk(day.theDate, 'date should not be set if null'); + t.equal(day.theDate, null); + + day = new Day({ theDate: undefined }); + t.notOk(day.theDate, 'date should not be set if undefined'); + t.equal(day.theDate, undefined); + + var Day2 = State.extend({ + props: { + theDate: { + type: 'date', + required: true, + allowNull: false + } + } + }); + + t.throws(function () { + new Day2({ theDate: null }); + }, /cannot be null/, 'if allowNull:false, and required:true should still throw'); + + t.end(); +}); + test('#68, #110 mixin props should not be deleted', function (t) { var SelectedMixin = { session : {