2012-02-12 64 views
0

使用ExtJS4 Store,我想要执行一个Javascript函数来返回json数据,即。 getMyJsonData,将数据加载到商店中。ExtJS4 Ext.data.Store从函数中加载数据

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的原因。 并通过内线JSONP代理去(因为它是跨域)使事情10X更复杂,因为我得LinkedIn AUTH等等等等(我不知道该怎么做还)

即 VAR MYDATA = NULL ;

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

     mydata = result; 
    }); 

} 
+0

你是什么意思?你想通过ajax获取数据吗? – Hadas 2012-02-12 13:59:19

+0

我想调用函数的原因是因为我想执行LinkedIn API。 并且通过Ext JSONP Proxy(因为它跨域)使得事情变得更加复杂10倍,因为我必须获得LinkedIn身份验证等等(我不知道该怎么做) – portoalet 2012-02-12 14:28:14

+0

您是否尝试过用户数据:yourfunction( )或类似的东西? – Hadas 2012-02-12 14:37:05

回答

0

使用代理加载数据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当你想改变的行为。

链接:

+0

我想调用函数的原因是因为我想执行LinkedIn API。 并且通过Ext JSONP Proxy(因为它跨域)使得事情变得更加复杂,因为我必须获得LinkedIn授权等等(我不知道该怎么做)。 ,我不明白extjs4如何可以做到这一点,是吗? – portoalet 2012-02-12 14:29:03

+1

@portoalet据我所知,我们可以做的是首先设置一个空的代理,当你的数据准备就绪时,使用setProxy来替换空的代理和准备好的内存代理。此外,有一个DirectProxy似乎有一个功能来完成数据请求:http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.proxy.Direct – liuyanghejerry 2012-02-12 14:39:20

+0

谢谢,我遵循http://stackoverflow.com/questions/6873534/extjs4-why-when-i-use-directfn-config-in-my-store-i-need-to-specify-directcfg-m,现在它工作。 – portoalet 2012-02-12 15:29:48