Jie*_*eng 8 extjs sencha-touch sencha-touch-2
在sencha touch 2中,似乎只有string,int,float,boolean数据类型.那么如何存储日期时间?
UPDATE
好的,我发现我可以convert()用来转换值:http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Types
convert:Function(函数)将原始数据值从数据块转换为要存储在Field中的数据的函数.该函数传递了collowing参数:
- v:Mixed
Reader读取的数据值,如果未定义,将使用配置的defaultValue.
- rec:Mixed
包含Reader读取的行的数据对象.根据Reader类型,它可以是Array(ArrayReader),对象(JsonReader)或XML元素.
// Add a new Field data type which stores a VELatLong object in the Record.
Ext.data.Types.VELATLONG = {
convert: function(v, data) {
return new VELatLong(data.lat, data.long);
},
sortType: function(v) {
return v.Latitude; // When sorting, order by latitude
},
type: 'VELatLong'
};
Run Code Online (Sandbox Code Playgroud)
但我真的不懂代码.对于convert(),什么设置参数?为什么第一个参数未被使用,何时以及它用于什么?如何获取/设置此类自定义类型(它是否成为v或data存在convert())?
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Types-property-DATE 哦,他是一个日期数据类型......
无论如何,在回答你的问题之前!(参见tl; dr获取日期数据类型示例)
http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Field-cfg-convert
一种函数,它将Reader提供的值转换为将存储在Model中的对象.它传递了以下参数:
v:混合
Reader读取的数据值,如果未定义,将使用配置的defaultValue.rec:Ext.data.Model
包含目前由Reader读取的Model的数据对象.请注意,此时可能无法完全填充模型,因为字段是按字段数组中定义的顺序读取的.
Ext.define('Dude', {
extend: 'Ext.data.Model',
fields: [
{name: 'locationInCity', convert: function(rawDataValue,record){
return record.location+', '+record.city //would be something like Sprooklyn,Springfield
}},
{name: 'firstname', mapping: 'name.first'},
{name: 'lastname', mapping: 'name.last'},
{name: 'city', defaultValue: 'homeless'},
'state',
{name: 'location', convert: location}
]
});
Run Code Online (Sandbox Code Playgroud)
啊,在这一点上,我找到了你的例子的来源;)
// Add a new Field data type which stores a VELatLong object in the Record.
Ext.data.Types.VELATLONG = {
convert: function(v, data) { // convert(value,record)
return new VELatLong(data.lat, data.long); //VELatLong was declared previously in another library as something according to example
},
sortType: function(v) {
return v.Latitude; // When sorting, order by latitude //VELatLong will have lat and long properties, this is for complex sorting
},
type: 'VELatLong' //This is what we use to reference it.
};
Run Code Online (Sandbox Code Playgroud)
所有这一切都或多或少地声明了一种新的数据类型.它会看起来像
// Add a new Field data type which stores a VELatLong object in the Record.
Ext.data.Types.tehDate = {
convert: function(v, data) { // convert(value,record)
return new date(v);
},
sortType: function(v) {
return v; // eh i have no idea whether its going to actually just accept date comparisons, though no there's real reason why it shouldn't
},
type: 'tehDate' //This is what we use to reference it.
};
^-- some of this is untested.
Run Code Online (Sandbox Code Playgroud)
TL; DR
现在实际回答你的原始问题:
Ext DOES有一个你可以使用的日期类型:Ext.data.Types.DATE(以及其他几个).
我假设类型:日期没有用,否则我们不会在这里!所以可能只有4个被正确引用.但!这确实有效:
var types = Ext.data.Types; // allow shorthand type access
Ext.define('Unit', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'dated', type: types.DATE },
{ name: 'pie', type: 'string' },
]
}
});
abc=Ext.create('Unit',{
dated: new Date().toString(),
pie:'hello'
})
console.log(abc)
console.log(abc.get('dated').getUTCFullYear())//it liiiiives!
Run Code Online (Sandbox Code Playgroud)
工作代码的小提琴:
http://www.senchafiddle.com/#w97Oe
sgo*_*les -3
Sencha Touch 1.0支持Model 中的日期类型。
但是,ModelSencha Touch 2只支持4 种数据类型。
您可以做的是,您可以将您的date格式设置String为格式,然后
date使用 API 将其转换为对象。