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

发布于 2025-01-04 09:52:18 字数 1448 浏览 2 评论 0原文

使用ExtJS4 Store,我想执行一个Javascript函数来返回json数据,即。 getMyJsonData,将数据加载到存储中。

function getMyJsonData() {
  var myjson = {... };
  return myjson;

}

浏览 doco,我找不到定义回调函数来加载数据的方法,我发现的只是一个加载已定义数据对象的内存存储。

我怎样才能调用一个函数呢?

 Ext.define('perhoo.store.Users',
        {
            extend: 'Ext.data.Store',
            model: 'perhoo.model.User',
            autoLoad: true,


                data : {
                users: [
                    { id: 1, name: 'joe44', email: '[email protected]'},
                    { id: 2, name: 'bloggs44', email: '[email protected]'}
                    ]
            },

            proxy: {
                type: 'memory',
                data: this.data,
                reader: {
                    type : 'json',
                    root : 'users'
                }
            }

编辑

我想调用函数的原因是因为我想执行 LinkedIn API。 通过 Ext JSONP 代理(因为它是跨域)使事情变得复杂 10 倍,因为我必须获得 LinkedIn 身份验证等(我还不知道如何做到这一点),

即 var mydata = null;

function onLinkedInAuth() {
   // Linked in api to find connections
   IN.API.Connections("me").result( function(result) { 

        mydata = result;
   } );

}

Using ExtJS4 Store, I want to execute a Javascript function to return json data, ie. getMyJsonData, to load the data into the store.

function getMyJsonData() {
  var myjson = {... };
  return myjson;

}

Trawling through the doco, I can't find a way to define a callback function to load the data, all I found was a Memory Store which loads an already defined data object.

How can I call a function instead ?

 Ext.define('perhoo.store.Users',
        {
            extend: 'Ext.data.Store',
            model: 'perhoo.model.User',
            autoLoad: true,


                data : {
                users: [
                    { id: 1, name: 'joe44', email: '[email protected]'},
                    { id: 2, name: 'bloggs44', email: '[email protected]'}
                    ]
            },

            proxy: {
                type: 'memory',
                data: this.data,
                reader: {
                    type : 'json',
                    root : 'users'
                }
            }

EDIT

The reason I want to call a function is because I want to execute LinkedIn API.
And going through Ext JSONP Proxy (as it's cross domain) makes things 10x more complicated as I have to get LinkedIn auth etc etc (which I don't know how to do it yet)

i.e.
var mydata = null;

function onLinkedInAuth() {
   // Linked in api to find connections
   IN.API.Connections("me").result( function(result) { 

        mydata = result;
   } );

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-01-11 09:52:18

ExtJS4的store使用代理加载数据,如下所示:

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来更改您想要的行为。

链接:

ExtJS4's store using proxy to load data, check below:

var myStore = Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : '/users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    },
    autoLoad: true
});

When executed, it reads data from the url, /users.json, and store into itself.

Also, if you want to process data yourself and load them into store yourself, you can check below:

//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'
        }
    }
});

And you could easily usesetProxy to change the behavior when you want.

Links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文