PDA

View Full Version : Override default dateFormat



KristofTorfs
26 Dec 2010, 9:22 PM
Hi,

I'm trying to set some global defaults so I don't have to specify a reader for every form, as well as not mentioning the dateFormat for every DateField.

The code I currently use, (and would think is correct as well) is this:



Ext.apply(Ext.form.DateField.prototype, {
format: 'd-m-Y G:i'
});
Ext.apply(Ext.data.Field.prototype, {
dateFormat: 'U'
});


However, when I load my forms without setting a reader my DateFields do get the correct date (in the correct format), but as time they always get 0:00. Enabling the reader (code below) fixes this problem.



reader: new Ext.data.JsonReader({
root: function(v) {
return v.data ? [v.data] : [];
},
successProperty: 'success',
fields: [{
name: 'model'
},{
name: 'id', type: 'int'
},{
name: 'filename'
},{
name: 'owner'
},{
name: 'created', type: 'date', dateFormat: 'U'
},{
name: 'modified', type: 'date', dateFormat: 'U'
},{
name: 'revision'
},{
name: 'published', type: 'bool'
}]
})


Any ideas why the DateField override works, and the Field override doesn't?

Thanks in advance!

Condor
26 Dec 2010, 11:05 PM
1. A DateField isn't meant to be used for time editing.

2. If you load a form without a reader, it will try to initialize the fields with the raw values from the server (in 'U' format in your case).
Your datafield will need to contain (at least) 'U' in its 'altFormats' config option to be able to interpret that value.

KristofTorfs
26 Dec 2010, 11:18 PM
For your first point, I actually didn't think about that at all, considering the fields are read-only :)

However, I tried specifying 'U' in altFormats and this didn't help either. Am I to understand that the DateField just isn't sufficient when handling times, even for displaying? I'll just have to take a look at which field(s) I'd use best.

One more question though: the code I used to change the Ext.data.Field prototype is correct? So if should decide to change the format of dates in my PHP backend, I would only have to change it there, and my entire application would remain function.

Condor
26 Dec 2010, 11:41 PM
However, I tried specifying 'U' in altFormats and this didn't help either. Am I to understand that the DateField just isn't sufficient when handling times, even for displaying? I'll just have to take a look at which field(s) I'd use best.
DateField detects if the format contains 'g', 'G', 'h' or 'H' to determine if time should be parsed. Unfortunately, it doesn't take 'U' into account, so it assumes it should only parse the date and not the time. I created a bugreport (http://www.sencha.com/forum/showthread.php?119610-DateField.safeParse-and-c-or-U-format) for this..


One more question though: the code I used to change the Ext.data.Field prototype is correct? So if should decide to change the format of dates in my PHP backend, I would only have to change it there, and my entire application would remain function.
Yes, but do remember that you still have to specify type:'date' for all date fields.