-
17 Mar 2013 1:02 PM #1
2.1/2.2: WebStorage proxies corrupt timestamp/time date formats
2.1/2.2: WebStorage proxies corrupt timestamp/time date formats
Ext.data.proxy.WebStorage uses code like this to encode/decode date fields:
If dateFormat is "time" or "timestamp", it will use that as the format string, producing timestamps like this "300004e0030am04p"Code:dateFormat = field.getDateFormat(); if (dateFormat) { data[name] = Ext.Date.format(rawData[name], dateFormat); } else { data[name] = rawData[name].getTime(); }
Ext.data.writer.Writer and Ext.data.proxy.Sql both have similar-but-not-identical writeDate methods that have smarts for this:
How about moving that logic into field and let it worry about encoding/decoding itself?Code:// Ext.data.writer.Writer writeDate: function(field, date) { if (!date) { return null; } 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); } } // Ext.data.proxy.Sql writeDate: function (field, date) { if (Ext.isEmpty(date)) { return null; } var dateFormat = field.getDateFormat() || this.getDefaultDateFormat(); switch (dateFormat) { case 'timestamp': return date.getTime() / 1000; case 'time': return date.getTime(); default: return Ext.Date.format(date, dateFormat); } }
getRecord has the same problem, but the fix there is even simpler -- leave the date alone and let the model convert it.
Here's my patch for 2.1.1, may work for 2.2b also:
Code:Ext.define('Jarvus.patch.WebStorageDates', { override: 'Ext.data.proxy.WebStorage', // respect 'timestamp' and 'time' dateFormats getRecord: function(id) { if (this.cache[id] === undefined) { var recordKey = this.getRecordKey(id), item = this.getStorageObject().getItem(recordKey), data = {}, Model = this.getModel(), fields = Model.getFields().items, length = fields.length, i, field, name, record, rawData, dateFormat; if (!item) { return undefined; } rawData = Ext.decode(item); for (i = 0; i < length; i++) { field = fields[i]; name = field.getName(); if (typeof field.getDecode() == 'function') { data[name] = field.getDecode()(rawData[name]); } else { data[name] = rawData[name]; } } record = new Model(data, id); this.cache[id] = record; } return this.cache[id]; }, // respect 'timestamp' and 'time' dateFormats setRecord: function(record, id) { if (id) { record.setId(id); } else { id = record.getId(); } var me = this, rawData = record.getData(), data = {}, Model = me.getModel(), fields = Model.getFields().items, length = fields.length, i = 0, field, name, obj, key, dateFormat; for (; i < length; i++) { field = fields[i]; name = field.getName(); if (field.getPersist() === false) { continue; } if (typeof field.getEncode() == 'function') { data[name] = field.getEncode()(rawData[name], record); } else { if (field.getType().type == 'date' && Ext.isDate(rawData[name])) { dateFormat = field.getDateFormat() || 'timestamp'; switch (dateFormat) { case 'timestamp': data[name] = rawData[name].getTime()/1000; break; case 'time': data[name] = rawData[name].getTime(); break; default: data[name] = Ext.Date.format(rawData[name], dateFormat); } } else { data[name] = rawData[name]; } } } obj = me.getStorageObject(); key = me.getRecordKey(id); //keep the cache up to date me.cache[id] = record; //iPad bug requires that we remove the item before setting it obj.removeItem(key); try { obj.setItem(key, Ext.encode(data)); } catch(e){ this.fireEvent('exception', this, e); } record.commit(); } });
-
18 Mar 2013 6:19 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,121
- Vote Rating
- 453
Thanks for the report! I have opened a bug in our bug tracker.
Success! Looks like we've fixed this one. According to our records the fix was applied for
TOUCH-4145
in
Sprint 32.


Reply With Quote