[英]ExtJS4 Ext.data.Store Load data from a function
我想使用ExtJS4 Store執行Javascript函數以返回json數據,即。 getMyJsonData,以將數據加載到存儲中。
function getMyJsonData() {
var myjson = {... };
return myjson;
}
遍歷doco時,我找不到定義加載數據的回調函數的方法,我發現的全部是一個Memory Store,它加載一個已定義的數據對象。
我該如何調用函數呢?
Ext.define('perhoo.store.Users',
{
extend: 'Ext.data.Store',
model: 'perhoo.model.User',
autoLoad: true,
data : {
users: [
{ id: 1, name: 'joe44', email: 'joe@joe.com'},
{ id: 2, name: 'bloggs44', email: 'bloggs@joe.com'}
]
},
proxy: {
type: 'memory',
data: this.data,
reader: {
type : 'json',
root : 'users'
}
}
編輯
我要調用函數的原因是因為我要執行LinkedIn API。 並且通過Ext JSONP代理(因為它是跨域的)使事情變得十分復雜,因為我必須獲得LinkedIn身份驗證等(我尚不知道如何做到)
即var mydata = null;
function onLinkedInAuth() {
// Linked in api to find connections
IN.API.Connections("me").result( function(result) {
mydata = result;
} );
}
ExtJS4的商店使用代理加載數據,請檢查以下內容:
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : '/users.json',
reader: {
type: 'json',
root: 'users'
}
},
autoLoad: true
});
執行后,它將從url
/users.json
讀取數據,並將其存儲到自身中。
另外,如果您想自己處理數據並將其加載到存儲中,則可以進行以下檢查:
//this is the model we will be using in the store
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'phone', type: 'string', mapping: 'phoneNumber'}
]
});
//this data does not line up to our model fields - the phone field is called phoneNumber
var data = {
users: [
{
id: 1,
name: 'Ed Spencer',
phoneNumber: '555 1234'
},
{
id: 2,
name: 'Abe Elias',
phoneNumber: '666 1234'
}
]
};
//note how we set the 'root' in the reader to match the data structure above
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
model: 'User',
data : data,
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'users'
}
}
});
而且,您可以輕松使用setProxy
更改所需的行為。
鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.