javascript - 将单个对象加载到 sencha touch 中的数据存储中

标签 javascript ruby-on-rails json sencha-touch extjs

我正在尝试使用 sencha touch 加载单个 json 对象。 当我使用数组时,一切都很好,但我还没有找到将单个对象加载到 Ext.data.Store 中的方法

这是我尝试加载的示例:

{"person":{"name":"John","surname":"Fox"}}

而且它不工作。 看了之后 this入口,

我尝试加载以下内容并且成功了:

[{"person":{"name":"John","surname":"Fox"}}]

我的问题是:有没有一种方法可以不使用 [ ] 来加载它? 为了做到这一点,我不得不修改我的服务器端代码,这对我来说就像一种代码味道...... 我希望能够加载单个 json 对象而不将其放入列表中。

这是我的 Sencha Touch 代理代码:

Ext.regModel("Person", {
    fields: ['name','surname']

});

var store = new Ext.data.Store({


            model : "Person",
            autoLoad : true,

            proxy: {
                type: 'ajax',
                url : 'my json url...',
                reader: {
                    type: 'json',
                    record: 'person'
                }
            }
        });

顺便说一句 - 我的服务器端代码在 Ruby on Rails 中。

最佳答案

Ext 开发团队的 Animal 给出了这个非常有用的解决方案:

reader: {
    type: 'json',
    root: function(data) {
        if (data.users) {
            if (data.users instanceof Array) {
                return data.users;
            } else {
                return [data.users];
            }                    
        }                
    }
}

通过简单地使用一个函数作为根,您可以确保读者获得数组中的对象。

关于javascript - 将单个对象加载到 sencha touch 中的数据存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5791684/

相关文章:

javascript - 如何在 api 的链式执行期间制作 frisby.js 脚本 "pause"

ruby-on-rails - 将 has_many 成员分组为 postgres db 中的 member_ids 数组

java - Java 中的 JSON : Using encoded JSON files and keeping it

javascript - 如何使用脚本通过电子邮件发送已解析的 JSON

javascript - jQuery 中的鼠标悬停事件

javascript - Meteor Accounts.loginWithPassword "400 Match Failed"错误

ruby-on-rails - 是否有允许我只定义一次模型的框架?

ruby-on-rails - 与 has_many 建立关联 :through in multiple models

json - 如何使用 Decodable 解码 JSON 数组?

javascript - 在 Angular 中使用插件导入 svg.js