PDA

View Full Version : Model Time Field -> Form Date Field -> Model Time Field



Bleak
27 Mar 2014, 9:53 AM
I find this scenario very confusing, and I'm sure I am simply missing something evident. I have a data model that is defined as;



Ext.define('DateRange', {
extend: 'Ext.data.Model',
{
name:'effectiveDate',
type: 'time',
dateFormat: 'time'
}
);


It reads in a json payload such as;



{
effective_Date: "1321938000000"
}


I want to display this on a Form Panel in a date field, like this;



{
xtype: 'datefield',
fieldLabel: 'Effective Date',
name: 'effectiveDate',
format: 'Y/m/d'
}



When I end up submitting this data back to the model, I want the date to be in a format of time, but I want the user to interact with it it as a Y/m/d format..

I must be missing something between format, submitFormat... etc..

Thanks, and please help!

israelroldan
2 Apr 2014, 11:37 AM
There's no 'time' type for Ext.data.Field, you need to use 'date' instead. Here's an example: https://fiddle.sencha.com/#fiddle/4nu

Bleak
2 Apr 2014, 11:53 AM
That is interesting, not sure where I got the time type. I'm assuming Ext is ignoring the type then, as everything is working properly now and I've managed to get the server -> store -> model -> form -> model -> store -> server all formatting and displaying properly.

For those that are curious, on my Form I have a field definition



{
xtype: 'datefield',
fieldLabel: 'Effective Date',
name: 'effectiveDate',
format: 'Y/m/d',
altFormats: 'time'
}



This allows the datefield to render the time they way I like, as it will parse the date based on the altFormats and then use the format for display. However, when the Model is updated from the form form.updateRecord(rec); The time is stored in an incorrect format. So, on the model, I have a serialize method as follows;



/**
* Converting from MS Time, to preferred format
*/
serialize : function(v, record) {
var d1 = new Date(v);
var newDate = Ext.Date.format(d1, 'time');
return newDate;
}


It could use some error checking, but everything working nicely now. The thing that allowed me to finally wrap my head around it is that EVERY time you change a models value it would run the convert method.. so my Model assumes that the date field will ALWAYS be a time format (MS) so when it is serialized, I just ensure it goes back out that way.

Hope this helps some.