1、Record
首先需要明確是,ExtJS中有一個名為Record的類,表格等控件中使用的數(shù)據(jù)是存放在Record對象中,一個Record可以理解為關(guān)系數(shù)據(jù)表中的一行,也可以稱為記錄。Record對象中即包含了記錄(行中各列)的定義信息(也就是該記錄包含哪些字段,每一個字段的數(shù)據(jù)類型等),同時又包含了記錄具體的數(shù)據(jù)信息(也就是各個字段的值)。
我們來看直接使用Record的代碼:
var MyRecord = Ext.data.Record.create([
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'},
{name: 'lastLoginTime', mapping: 'loginTime', type: 'date'}
]);
var r=new MyRecord({
title:"日志標(biāo)題",
username:"easyjf",
loginTimes:100,
loginTime:new Date()
});
alert(MyRecord.getField("username").mapping);
alert(MyRecord.getField("lastLoginTime").type);
alert(r.data.username);
alert(r.get("loginTimes"));
});
首先使用Record的create方法創(chuàng)建一個記錄集MyRecord,MyRecord其實(shí)是一個類,該類包含了記錄集的定義信息,可以通過MyRecord來創(chuàng)建包含字段值的Record對象。在上面的代碼中,最后的幾條語句用來輸出記錄集的相關(guān)信息,MyRecord.getField("username")可以得到記錄中username列的字段信息,r.get("loginTimes")可以得到記錄loginTimes字段的值,而r.da
對Record有了一定的了解,那么要操作記錄集中的數(shù)據(jù)就非常簡單了,比如r.set(name,value)可以設(shè)置記錄中某指定字段的值,r. dirty可以得到當(dāng)前記錄是否有字段的值被更改過等等。
2、Store
Store可以理解為數(shù)據(jù)存儲器,可以理解為客戶端的小型數(shù)據(jù)表,提供緩存等功能。在ExtJS中,GridPanel、ComboBox、DataView等控件一般直接與Store打交道,直接通過store來獲得控件中需要展現(xiàn)的數(shù)據(jù)等。一個Store包含多個Record,同時Store又包含了數(shù)據(jù)來源,數(shù)據(jù)解析器等相關(guān)信息,Store通過調(diào)用具體的數(shù)據(jù)解析器(DataReader)來解析指定類型或格式的數(shù)據(jù)(DataProxy),并轉(zhuǎn)換成記錄集的形式保存在Store中,作為其它控件的數(shù)據(jù)輸入。
數(shù)據(jù)存儲器由Ext.da
var MyRecord = Ext.data.Record.create([
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'},
{name: 'lastLoginTime', mapping: 'loginTime', type: 'date'}
]);
var dataProxy=new Ext.data.HttpProxy({url:"link.ejf"});
var theReader=new Ext.data.JsonReader({
totalProperty: "results",
root: "rows",
id: "id"
},MyRecord);
var store=new Ext.data.Store({
proxy:dataProxy,
reader:theReader
});
store.load();
當(dāng)然,這樣的難免代碼較多,Store中本身提供了一些快捷創(chuàng)建Store的方式,比如上面的示例代碼中可以不用先創(chuàng)建一個HttpProxy,只需要在創(chuàng)建Store的時候指定一個url配置參數(shù),就會自動使用HttpProxy來加載參數(shù)。比如,上面的代碼可以簡化成:
var MyRecord = Ext.data.Record.create([
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'},
{name: 'lastLoginTime', mapping: 'loginTime', type: 'date'}
]);
var theReader=new Ext.data.JsonReader({
totalProperty: "results",
root: "rows",
id: "id"
},MyRecord);
var store=new Ext.data.Store({
url:"link.ejf",
proxy:dataProxy,
reader:theReader
});
store.load();
雖然不再需要手動創(chuàng)建HttpProxy了,但是仍然需要創(chuàng)建DataReader等,畢竟還是復(fù)雜,ExtJS進(jìn)一步把這種常用的數(shù)據(jù)存儲器進(jìn)行了封裝,在Store類的基礎(chǔ)上提供了SimpleStore、SimpleStore、GroupingStore等,直接使用SimpleStore,則上面的代碼可以進(jìn)一步簡化成下面的內(nèi)容:
var store=new Ext.data.JSonStore({
url:"link.ejf?cmd=list",
totalProperty: "results",
root: "rows",
fields:['title', {name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'},
{name: 'lastLoginTime', mapping: 'loginTime', type: 'date'}
]
});
store.load();
3、DataReader
DataReader表示數(shù)據(jù)讀取器,也就是數(shù)據(jù)解析器,其負(fù)責(zé)把從服務(wù)器或者內(nèi)存數(shù)組、xml文檔中獲得的雜亂信息轉(zhuǎn)換成ExtJS中的記錄集Record數(shù)據(jù)對象,并存儲到Store里面的記錄集數(shù)組中。
數(shù)據(jù)解析器的基類由Ext.da
1)ArrayReader
Ext.da
var MyRecord = Ext.data.Record.create([
{name: 'title', mapping:1},
{name: 'username', mapping:2},
{name: 'loginTimes', type:3}
]);
var myReader = new Ext.data.ArrayReader({
id: 0
}, MyRecord);
這里定義的myReader可以讀取下面的二維數(shù)組:
[ [1, '測試', '小王',3], [2, '新年好', 'williamraym',13] ]
2)JsonReader
Ext.da
var MyRecord = Ext.data.Record.create([
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'}
]);
var myReader = new Ext.data.JsonReader({
totalProperty: "results",
root: "rows",
id: "id"
}, MyRecord);
這里的JsonReader可以解析下面的JSON數(shù)據(jù):
{ 'results': 2, 'rows': [
{ id: 1, title: '測試', author: '小王', loginTimes: 3 },
{ id: 2, title: 'Ben', author: 'williamraym', loginTimes:13} ]
}
JSonReader還有比較特殊的用法,就是可以把Store中記錄集的配置信息存放直接保存在從服務(wù)器端返回的JSON數(shù)據(jù)中,比如下面的例子:
var myReader = new Ext.data.JsonReader();
這一個不帶任何參數(shù)的myReader,可以處理從服務(wù)器端返回的下面JSON數(shù)據(jù):
{
'metaData': {
totalProperty: 'results',
root: 'rows',
id: 'id',
fields: [
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'} ]
},
'results': 2, 'rows': [
{ id: 1, title: '測試', author: '小王', loginTimes: 3 },
{ id: 2, title: '新年好', author: 'williamraym', loginTimes:13}]
}
3)XmlReader
Ext.da
var MyRecord = Ext.data.Record.create([
{name: 'title'},
{name: 'username', mapping: 'author'},
{name: 'loginTimes', type: 'int'}
]);
var myReader = new Ext.data.XmlReader({
totalRecords: "results",
record: "rows",
id: "id"
}, MyRecord);
上面的myReader能夠解析下面的xml文檔信息:
<topics>
<results>2</results>
<row>
<id>1</id>
<title>測試</ title >
<author>小王</ author >
<loginTimes>3</ loginTimes >
</row>
<row>
<id>2</id>
<title>新年好</ title >
<author> williamraym </ author >
<loginTimes>13</ loginTimes >
</row>
</topics>