Sencha Touch 1.1.1 嵌套 JSON 存储加载

发布于 2024-12-22 11:16:08 字数 2230 浏览 4 评论 0原文

大家好,我希望有人能帮助我解决这个问题,虽然这是一些该死的初学者问题,但已经得到了回答,但我真的很困惑,我向你们保证,我阅读了所有答案帖子,但仍然无法让它发挥作用。

我正在使用 Sencha Touch 1.1.1 并尝试让此 Store 加载嵌套的 JSON 数据。代码如下:

    Ext.regModel("UserData", {

        hasMany : [{
            name : "id",
            type : "integer",
        },{
            name : "username",
            type : "string",
        },{
            name : "password",
            type : "string",
        }]

    });

    var userdata = 
        {"users": [
                  {
                      "id": 16,
                      "username": "[email protected]",
                      "password": "bla",
                  }, {
                      "id": 17,
                      "username": "[email protected]",
                      "password": "bla",
                  }
                 ]
        };


    var myStore = new Ext.data.Store({

        model : 'UserData',
        data : userdata,
        proxy : {
            type : 'ajax',             

            reader : {
                type : 'json',
                root : 'users'      // not working          
            }
        }

    });

    var myList = new Ext.List ({

        fullscreen : true,
        store : myStore,
        grouped : false,
        itemTpl : '<div>{username}</div>'

    });

返回未捕获的类型错误:参数列表的类型错误。当我使用外部数组包装器重写 JSON 时,它可以工作,但使用错误的根(不是用户)时,我确实看到了与 root:'' 值一起使用的示例。

var userdata = 
    [ {"users": [
              {
                  "id": 16,
                  "username": "[email protected]",
                  "password": "bla",
              }, {
                  "id": 17,
                  "username": "[email protected]",
                  "password": "bla",
              }
             ]
    } ];

我缺少什么?

Hi guys I hope someone can help me with this I´m really stuck although it´s some damned beginner question that has already been answered and I assure you I read all of the answer Posts but still can´t get it to work.

I´m using Sencha Touch 1.1.1 and try to get this Store loaded with nested JSON data. Here´s the code:

    Ext.regModel("UserData", {

        hasMany : [{
            name : "id",
            type : "integer",
        },{
            name : "username",
            type : "string",
        },{
            name : "password",
            type : "string",
        }]

    });

    var userdata = 
        {"users": [
                  {
                      "id": 16,
                      "username": "[email protected]",
                      "password": "bla",
                  }, {
                      "id": 17,
                      "username": "[email protected]",
                      "password": "bla",
                  }
                 ]
        };


    var myStore = new Ext.data.Store({

        model : 'UserData',
        data : userdata,
        proxy : {
            type : 'ajax',             

            reader : {
                type : 'json',
                root : 'users'      // not working          
            }
        }

    });

    var myList = new Ext.List ({

        fullscreen : true,
        store : myStore,
        grouped : false,
        itemTpl : '<div>{username}</div>'

    });

Returns Uncaught Type Error: Arguments list has wrong type. When I rewrite the JSON with an outer Array wrapper, it works, but with wrong root (not users) I definitly saw examples where this worked with the root:'' value.

var userdata = 
    [ {"users": [
              {
                  "id": 16,
                  "username": "[email protected]",
                  "password": "bla",
              }, {
                  "id": 17,
                  "username": "[email protected]",
                  "password": "bla",
              }
             ]
    } ];

What am I missing?

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

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

发布评论

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

评论(1

烦人精 2024-12-29 11:16:08

如果我没有记错的话,在您的“UserData”模型中,它应该是 fields 而不是 hasMany

尝试将 json 数据放入单独的 json 文件中,并在商店代理中找到路径。

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

我测试了它,在这里工作得很好。这是我的完整代码。

    Ext.regModel("UserData", {

    fields: [
            {name: 'id', type:'int'},
            {name: 'username', type:'string'},
            {name: 'password', type:'string'}

    ]
});


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


var app = new Ext.Application({
    name: 'TestApp',
    useLoadMask: true,
    launch: function() {


        TestApp.views.listToolbar = new Ext.Toolbar({
            title: 'Foo Bar',
            layout: 'hbox'
        });

        TestApp.views.list = new Ext.List({
            id: 'list',
            store: myStore,
            emptyText: 'Nothing',
            itemTpl: '<div class="username">{username}</div>',
        });

        TestApp.views.container = new Ext.Panel({
            layout: 'fit',
            html: 'this is the container',
            dockedItems: [TestApp.views.listToolbar],
            items: [TestApp.views.list]
        });

        TestApp.views.viewport = new Ext.Panel({
            fullscreen: true,
            layout: 'card',
            cardAnimation: 'slide',
            items: [
                TestApp.views.container
            ]
        });
    }
});

If I am not mistaken, in your "UserData" model, it should be fields instead of hasMany.

And try putting your json data in a separate json file and locate the path in your store's proxy.

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

I tested it and it's working fine here. Here is my full code.

    Ext.regModel("UserData", {

    fields: [
            {name: 'id', type:'int'},
            {name: 'username', type:'string'},
            {name: 'password', type:'string'}

    ]
});


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


var app = new Ext.Application({
    name: 'TestApp',
    useLoadMask: true,
    launch: function() {


        TestApp.views.listToolbar = new Ext.Toolbar({
            title: 'Foo Bar',
            layout: 'hbox'
        });

        TestApp.views.list = new Ext.List({
            id: 'list',
            store: myStore,
            emptyText: 'Nothing',
            itemTpl: '<div class="username">{username}</div>',
        });

        TestApp.views.container = new Ext.Panel({
            layout: 'fit',
            html: 'this is the container',
            dockedItems: [TestApp.views.listToolbar],
            items: [TestApp.views.list]
        });

        TestApp.views.viewport = new Ext.Panel({
            fullscreen: true,
            layout: 'card',
            cardAnimation: 'slide',
            items: [
                TestApp.views.container
            ]
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文