Last active
January 31, 2017 19:19
-
-
Save jratcliff/8ba6238b51eeb3a68fc5a72acab6a7b4 to your computer and use it in GitHub Desktop.
Ext.data.writer.Writer override
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Overrides for Ext.data.writer.Writer | |
*/ | |
Ext.define('overrides.data.writer.Writer', { | |
override: 'Ext.data.writer.Writer', | |
/** | |
* Override that allows the 'serialize' option to be honored if it is passed | |
* in the allDataOptions or partialDataOptions configs of a writer's config. | |
* JIRA: EXTJS-17166 | |
*/ | |
getRecordData: function (record, operation) { | |
var me = this, | |
nameProperty = me.getNameProperty(), | |
mapping = nameProperty !== 'name', | |
idField = record.self.idField, | |
key = idField[nameProperty] || idField.name, // setup for idField first | |
value = record.id, | |
writeAll = me.getWriteAllFields(), | |
ret, dateFormat, phantom, | |
options, clientIdProperty, | |
fieldsMap, data, field; | |
if (idField.serialize) { | |
value = idField.serialize(value); | |
} | |
if (!writeAll && operation && operation.isDestroyOperation) { | |
ret = {}; | |
ret[key] = value; | |
} else { | |
dateFormat = me.getDateFormat(); | |
phantom = record.phantom; | |
options = (phantom || writeAll) ? me.getAllDataOptions() : me.getPartialDataOptions(); | |
clientIdProperty = phantom && me.getClientIdProperty(); | |
fieldsMap = record.getFieldsMap(); | |
// OVERRIDE BEGIN | |
//options.serialize = false; // we must take over this here | |
// OVERRIDE END | |
data = record.getData(options); | |
// If we are mapping we need to pour data into a new object, otherwise we do | |
// our work in-place: | |
ret = mapping ? {} : data; | |
if (clientIdProperty) { // if (phantom and have clientIdProperty) | |
ret[clientIdProperty] = value; // must read data and write ret | |
delete data[key]; // in case ret === data (must not send "id") | |
} | |
else if (!me.getWriteRecordId()) { | |
delete data[key]; | |
} | |
for (key in data) { | |
if (data.hasOwnProperty(key)) { | |
value = data[key]; | |
if (!(field = fieldsMap[key])) { | |
// No defined field, so clearly no nameProperty to look up for this field | |
// but if we are mapping we need to copy over the value. Also there is no | |
// serializer to call in this case. | |
if (mapping) { | |
ret[key] = value; | |
} | |
} else { | |
// OVERRIDE - add a !options.serialize check so that we don't serialize a date twice | |
if (!options.serialize) { | |
// Allow this Writer to take over formatting date values if it has a | |
// dateFormat specified. Only check isDate on fields declared as dates | |
// for efficiency. | |
if (field.isDateField && dateFormat && Ext.isDate(value)) { | |
value = Ext.Date.format(value, dateFormat); | |
} else if (field.serialize) { | |
value = field.serialize(value, record); | |
} | |
} | |
if (mapping) { | |
key = field[nameProperty] || key; | |
} | |
ret[key] = value; | |
} | |
} | |
} | |
} | |
return ret; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment