【问题标题】:ExtJS store initialization using json fails使用 json 的 ExtJS 存储初始化失败
【发布时间】:2012-11-21 14:59:58
【问题描述】:

我有一个这样的商店:

Ext.define('app.store.System', {
extend : 'Ext.data.Store',
model : 'app.model.System',
autoLoad: true,
autoSync: true,

proxy: {
    type: 'rest',
    format: 'json',
    url: '/application/appinfo',
    method : "GET",
    reader: {
        type: 'json',
        root: 'System'
    },
    writer: {
        root: 'System',
        allowSingle: false
    }
}
});

并且我有一个服务端点来处理使用此方法匹配 /application 的请求:

@GET
@Path("/{sysinfo}")
public List<SystemInfo> getSystemInfo() {        
    if(sysinfo == null){
        sysinfo = new SystemInfo();

        ...initialize
    }

    List<SystemInfo> resultList = new ArrayList<SystemInfo>();
    resultList.add(sysinfo);

    return resultList;
}

它似乎工作......当我试图到达 localhost:port/application/sysinfo.json 我得到了这个:

{ [{"address":"...","feeds":["feed1","feed2"] } ] }

这似乎是正确的,但是当我尝试在视图的 init 方法中从存储中读取数据时:

var store = Ext.StoreManager.lookup('System');
    var data = [];

    store.each(function(record) {
        data.push(record.getData());
        console.log("record data: ");
        console.log(record.getData());
    });

    console.log(data[0]);

它说它是未定义的,就好像商店是空的一样。我用调试器试了一下,我发现 getSystemInfo() 在视图的 initcomponent 之后被调用 但不幸的是我不知道为什么会这样或如何解决它。有什么想法吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: javascript json rest extjs extjs4


    【解决方案1】:

    您是否尝试过先加载您的商店?

    var store = Ext.StoreManager.lookup('System');
    var data = [];
    store.load({
       callback: function(storeVar, records, successful) {
          Ext.each(records, function(record) {
             data.push(record.getData());
             console.log("record data: ");
             console.log(record.getData());
          });
       }
       console.log(data[0]);
    });
    

    boris 说的是真的,你需要在返回的 JSON 中定义你的根属性。

    如果您使用的是 chrome 或 firefox,您还可以检查进行了哪个网络调用,以及它返回了什么(JSON 数据...)。

    【讨论】:

      【解决方案2】:

      试试这个:

      return new { success: true, System = resultList};
      

      【讨论】:

      • 你的意思是我的 bean 返回的 json 应该是这样的: { [{"address":"...","feeds":["feed1","feed2"] } ] success :是的,系统=结果列表}?
      • 没有。像这样:{success:true, System:[{"address":"...","feeds":["feed1","feed2"] } ] }。试试我的代码
      • 确实,您的读者希望 System 成为您的根数据属性(包含所有对象的数组)。
      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多