繁体   English   中英

ExtJS4 Ext.data.Store从函数加载数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM