【问题标题】:Read nested XML to model with hasMany association, Sencha, ExtJS使用 hasMany 关联、Sencha、ExtJS 读取嵌套 XML 以建模
【发布时间】:2012-06-10 18:29:44
【问题描述】:

我正在尝试解析以下 xml(在我的本地计算机中托管之后)并将其加载到存储中。

<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user>
       <id>2</id>
       <name>shameel</name>
       <post>
         <user_id>5</user_id>
         <title>programmer</title>
         <body>nothing</body>
       </post>
       <post>
         <user_id>6</user_id>
         <title>newpost</title>
         <body>congrats</body>
       </post>
    </user> 
<user>
       <id>3</id>
       <name>abdulls</name>
       <post>
         <user_id>5</user_id>
         <title>programmer1</title>
         <body>nothing1</body>
       </post>
       <post>
         <user_id>6</user_id>
         <title>newpost1</title>
         <body>congrats1</body>
       </post>
       <post>
         <user_id>7</user_id>
         <title>newpost1</title>
         <body>congrats1</body>
       </post>
    </user>       
</users>

使用的型号如下:

Ext.define("SectionModel", {
            extend : 'Ext.data.Model',
            config : {
                fields : [{
                            name : 'section',
                            type : 'string',
                            mapping : '@section'
                        }],
                proxy : {
                    type : 'ajax', 

                     url : 'http://localhost:8080/sample1.xml', //sample1.xml is the file

                    reader : {
                        type : 'xml',
                        rootProperty : 'configs',
                        record : 'navigation'
                    }
                },

                hasMany : [{
                    model : 'ArticlesModel',
                    name : 'articlesModel',
                         associationKey:'sections'

                    }]



            }
        });










Ext.define("ArticlesModel", {
            extend : 'Ext.data.Model',
            config : {
                fields : [{
                        name : 'title',
                        type : 'string',
                        mapping : '@title'
                    }, {
                        name : 'value',
                        type : 'string',
                        mapping : '@value'
                    }],

                    proxy : {
                    type : 'ajax',
                                        url : 'http://localhost:8080/sample1.xml', //sample1.xml is the file
                    reader : {
                        type : 'xml',
                        rootProperty : 'navigation',
                        record : 'section'
                    }
                },

                belongsTo : 'SectionModel'


            }
        });

使用的商店如下:

Ext.define("SectionStore", {
            extend : 'Ext.data.Store',

            config : {
                model : 'SectionModel',
                autoLoad : 'true'



            }
        });

我尝试访问以下内容:

var store = Ext.data.StoreManager.get('SectionStore');
        if (!store) {
            console.log("Store not found");
            return;
        }

store.load(function(records, operation, success) {
                    for (var i = 0; i < store.getCount(); i++) {
                        var section_title = store.getAt(i).get('section');
                                                var subsection_val = store.getAt(i).articlesModel().get('title');//this function fails saying no "get" function for the object
}
});

在这里我可以获得section_title。但无法获取 subsection_val。我已经用商店中的代理尝试过这个,但无法修复它。有没有人可以帮忙。

【问题讨论】:

    标签: xml-parsing extjs4 sencha-touch-2 model-associations


    【解决方案1】:

    我在解析嵌套的 XML 元素时遇到了类似的问题。不幸的是,在对 XML Parsing 代码进行了多次修改之后,我最终放弃了。 (最终编写了一个对我们的 Web 服务的传递,将响应转换为 JSON)

    你可以在这里阅读我的(错误)冒险:

    http://www.sencha.com/forum/showthread.php?189537-Sencha-Touch-2.0-nested-XML-parsing

    【讨论】:

    • 是的 json 是选项。 XML 是如何产生问题的。我们将 XML 封装在一个 jsonp 中并得到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2019-06-25
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多