Sencha Touch - 通过嵌套循环访问关联模型存储 JSON

发布于 2024-10-14 04:59:07 字数 2254 浏览 7 评论 0原文

我已经在 Stack Overflow 上潜伏了一段时间,并找到了很多非常有用的答案。非常感谢社区!我希望不久之后能够贡献我自己的有用答案。

与此同时,我还有一个无法解决的问题。我正在使用 Sencha Touch 创建一个基于 Web 的电话应用程序,但在使用嵌套循环迭代某些 JSON 时遇到问题。我可以获取第一级数据,但不能获取嵌套在第一级中的项目。有一个有点相关的ExtJS线程,但我决定创建我自己的,因为 ExtJS 和 Touch 在微妙但重要的方面存在分歧。无论如何,这里有一些代码来显示我在哪里:

JSON(截断 - JSON 是 PHP/MYSQL 生成的,目前实际上有三个带有“标题”的子级别,我可以访问所有这些子级别。这是子级别“ items”,我无法迭代):

{
"lists": [
    {
        "title": "Groceries",
        "id": "1",
        "items": [
            {
                "text": "contact solution - COUPON",
                "listId": "1",
                "id": "4",
                "leaf": "true" 
            },
            {
                "text": "Falafel (bulk)",
                "listId": "1",
                "id": "161",
                "leaf": "true" 
            },
            {
                "text": "brita filters",
                "listId": "1",
                "id": "166",
                "leaf": "true" 
            }
        ] 
    } 
]

}

Store:

var storeItms = new Ext.data.Store({
    model: 'Lists',
    proxy: {
        type: 'ajax',
        method: 'post',
        url : LIST_SRC,
        extraParams: {action: 'gtLstItms'},
        reader: {
            type: 'json',
            root: 'lists'
        }
    }
});

工作循环:

storeItms.on('load', function(){
    var lstArr = new Array();
    storeItms.each(function(i) {
        var title = i.data.title;
        lstArr.push(i.data.title);
    });
    console.log(lstArr);
});

非工作嵌套循环:

storeItms.on('load', function(){
    var lstArr = new Array();
    storeItms.each(function(i) {
        var title = i.data.title;
        var id = i.data.id;
        title.items.each(function(l) {
            lstArr.push(l.data.text);
        });
    });
    console.log(lstArr);
});

非工作嵌套循环给我错误“无法调用未定义的方法‘each’”,参考“title”。 items.each...'

我怀疑这是因为我没有将 title 设置为设置键:值对的键,所以它只看到一个字符串列表...但我有点在损失。

我应该提到的是,该商店是通过两个相互关联的模型来填充的。我知道 Store 可以访问所有内容,因为我能够通过 XTemplate 进行嵌套迭代。

我们将不胜感激任何帮助,并希望不久之后以实物形式回报社区!”

-埃里克

I've been lurking on Stack Overflow for quite some time now, and have found quite a number of very helpful answers. Many thanks to the community! I hope to be able to contribute my own helpful answers before too long.

In the meantime, I have another issue I can't figure out. I am using Sencha Touch to create a Web-based phone app and I'm having trouble using a nested loop to iterate through some JSON. I can grab the first level of data, but not the items nested within that first level. There is a somewhat related ExtJS thread, but I decided to create my own since ExtJS and Touch diverge in subtle yet important ways. Anyway, here is some code to show where I am:

JSON (truncated - the JSON is PHP/MYSQL-generated, and there are currently actually three sub levels with "title", all of which I can access. It's the sub level "items" through which I can't iterate):

{
"lists": [
    {
        "title": "Groceries",
        "id": "1",
        "items": [
            {
                "text": "contact solution - COUPON",
                "listId": "1",
                "id": "4",
                "leaf": "true" 
            },
            {
                "text": "Falafel (bulk)",
                "listId": "1",
                "id": "161",
                "leaf": "true" 
            },
            {
                "text": "brita filters",
                "listId": "1",
                "id": "166",
                "leaf": "true" 
            }
        ] 
    } 
]

}

Store:

var storeItms = new Ext.data.Store({
    model: 'Lists',
    proxy: {
        type: 'ajax',
        method: 'post',
        url : LIST_SRC,
        extraParams: {action: 'gtLstItms'},
        reader: {
            type: 'json',
            root: 'lists'
        }
    }
});

Working Loop:

storeItms.on('load', function(){
    var lstArr = new Array();
    storeItms.each(function(i) {
        var title = i.data.title;
        lstArr.push(i.data.title);
    });
    console.log(lstArr);
});

Non-working Nested Loop:

storeItms.on('load', function(){
    var lstArr = new Array();
    storeItms.each(function(i) {
        var title = i.data.title;
        var id = i.data.id;
        title.items.each(function(l) {
            lstArr.push(l.data.text);
        });
    });
    console.log(lstArr);
});

The non-working nested loop gives me the error "Cannot call method 'each' of undefined", in reference to 'title.items.each...'

I suspect this is because I've not set title to be a key to set up a key:value pair, so it just sees a list of strings...but I'm kind of at a loss.

I should mention that the store is populated via two Models that have been associated with one another. I know that the Store can access everything because I am able to do nested iterating via an XTemplate.

Any help will be much appreciated and hopefully returned to the community in kind before too long!'

-Eric

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

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

发布评论

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

评论(3

两人的回忆 2024-10-21 04:59:07

埃里克,为什么要循环?

如果您的模型以与 JSON 嵌套相同的方式关联,那么您应该能够在商店上设置 autoLoad:true ,然后坐下来享受。

无论如何,假设您出于其他一些不相关的原因需要这些数组,问题是您正在尝试 .each

i.data.title.items

当然您应该迭代此外

i.data.items

,如果对象是模型,您可以使用 .get() 代替数据对象的:

var title = i.get('title);

Eric, why the loop?

If your models are associated in the same way that the JSON is nested, then you should just be able to set autoLoad:true on the store, sit back and enjoy.

Anyway, on the assumption that you are needing these arrays for some other unrelated reason, the problem is that you are trying .each on

i.data.title.items

Surely you should be iterating through

i.data.items

Also, if the object is a model, you can use .get() instead of the data object:

var title = i.get('title);
帥小哥 2024-10-21 04:59:07

使用新的 sencha touch 2 框架,您可以在模型中创建关联,就像返回 json 的方式完全相同。
检查 Sencha Touch 2 模型文档 其中告诉您可以在模型上使用各种配置选项。
您可以参考ST2嵌套列表的示例 .

希望这有帮助。

Using new sencha touch 2 framework, you can create associations within the models exactly the same way how your json is returned.
Check Sencha Touch 2 Model Document which tells you the various config options on Model.
You may refer to this example of ST2 Nested List .

Hope this helps.

末骤雨初歇 2024-10-21 04:59:07

“title”不是一个可枚举对象,它是一个字符串。要迭代字符串,您需要将其拆分以将其转换为数组。

另外,不要使用 Ext.each 尝试简单的 for (var x in obj) {} 或 for (var xc in obj.prop) {} 如果有效,那么 ext.each 方法也应该有效,但如果 ext 无法迭代它会悄悄地失败的对象。

"title" is not a enumerable object, its a string. To iterate a string you'll need to split it to convert it into an array.

Also, instead of using Ext.each try a simple for (var x in obj) {} or for (var xc in obj.prop) {} If that works then the ext.each method should work as well but if ext cannot iterate the object it will just quietly fail.

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