PDA

View Full Version : [INFOREQ] [ST 2.1.0-rc2] Sencha tries to apply date formatting for NULL date



KoSik
26 Oct 2012, 4:04 AM
Proxy writer tries to apply date formatting for the model field with type "date" even if value of this field = null.


Here is how model field declared:


{
name: 'dateCompleted',
type: 'date',
dateFormat: 'Y-m-d',
allowNull: true
}




Here is how it is handled in Writer.js:



writeDate: function(field, date) {
var dateFormat = field.getDateFormat() || 'timestamp';
switch (dateFormat) {
case 'timestamp':
return date.getTime()/1000;
case 'time':
return date.getTime();
default:
return Ext.Date.format(date, dateFormat);
}
},


/**
* Formats the data for each record before sending it to the server. This
* method should be overridden to format the data in a way that differs from the default.
* @param {Object} record The record that we are writing to the server.
* @return {Object} An object literal of name/value keys to be written to the server.
* By default this method returns the data property on the record.
*/
getRecordData: function(record) {
var isPhantom = record.phantom === true,
writeAll = this.getWriteAllFields() || isPhantom,
nameProperty = this.getNameProperty(),
fields = record.getFields(),
data = {},
changes, name, field, key, value;


if (writeAll) {
fields.each(function(field) {
if (field.getPersist()) {
name = field.config[nameProperty] || field.getName();
value = record.get(field.getName());
if (field.getType().type == 'date') {
value = this.writeDate(field, value);
}
data[name] = value;
}
}, this);
} else {
// Only write the changes
changes = record.getChanges();
for (key in changes) {
if (changes.hasOwnProperty(key)) {
field = fields.get(key);
if (field.getPersist()) {
name = field.config[nameProperty] || field.getName();
value = changes[key];
if (field.getType().type == 'date') {
value = this.writeDate(field, value);
}
data[name] = value;
}
}
}
if (!isPhantom) {
// always include the id for non phantoms
data[record.getIdProperty()] = record.getId();
}
}
return data;
}


Here is how it SHOULD BE handled not to cause fatal error:



writeDate: function(field, date) {
var dateFormat = field.getDateFormat() || 'timestamp';
switch (dateFormat) {
case 'timestamp':
return date.getTime()/1000;
case 'time':
return date.getTime();
default:
return Ext.Date.format(date, dateFormat);
}
},


/**
* Formats the data for each record before sending it to the server. This
* method should be overridden to format the data in a way that differs from the default.
* @param {Object} record The record that we are writing to the server.
* @return {Object} An object literal of name/value keys to be written to the server.
* By default this method returns the data property on the record.
*/
getRecordData: function(record) {
var isPhantom = record.phantom === true,
writeAll = this.getWriteAllFields() || isPhantom,
nameProperty = this.getNameProperty(),
fields = record.getFields(),
data = {},
changes, name, field, key, value;


if (writeAll) {
fields.each(function(field) {
if (field.getPersist()) {
name = field.config[nameProperty] || field.getName();
value = record.get(field.getName());
if (field.getType().type == 'date' && value !== null) {
value = this.writeDate(field, value);
}
data[name] = value;
}
}, this);
} else {
// Only write the changes
changes = record.getChanges();
for (key in changes) {
if (changes.hasOwnProperty(key)) {
field = fields.get(key);
if (field.getPersist()) {
name = field.config[nameProperty] || field.getName();
value = changes[key];
if (field.getType().type == 'date') {
value = this.writeDate(field, value);
}
data[name] = value;
}
}
}
if (!isPhantom) {
// always include the id for non phantoms
data[record.getIdProperty()] = record.getId();
}
}
return data;
}

mitchellsimoens
26 Oct 2012, 7:52 AM
Think this has already been fixed. Maybe a test case?