来自XML的Sencha Touch 2 TreeStore

时间:2012-06-13 12:00:07

标签: xml tree sencha-touch

我需要帮助'导致从xml创建TreeStore时遇到问题(存储用于NestedList)。 查看Javascript代码如下。

{
            xtype: 'nestedlist',
            title: 'Журнал',
            iconCls: 'bookmarks',
            displayField: 'title',
            store: {
                type: 'tree',
                model: 'App.model.magazineNumber',
                root: {
                    leaf: false
                },
                proxy: {
                    type: 'ajax',
                    url: 'http://localhost:57648/repo/dataMocks/contentListItems.xml',
                    reader: {
                        type: 'xml',
                        rootProperty: 'items',
                        record: 'magazineNumber'
                    }
                }
            }
}

模型定义

Ext.define('App.model.magazineNumber', {
extend: 'Ext.data.Model',
config: {
    fields:
        [
            'title',
            'description',
            {
                name: 'leaf',
                defaultValue: false
            }]
//this doesn't help
//        , hasMany: [{
//            model: 'App.model.magazineNumber',
//            name: 'items'
//        }]
//        , belongsTo: 'App.model.magazineNumber'
    }
});

和xml文件

<?xml version="1.0" encoding="utf-8" ?>
<items>
  <magazineNumber>
    <title>Первый номер</title>
    <description>Описание первого номера</description>
    <items>
      <magazineNumber>
        <title>Статья один</title>
        <description>Описание cтатьи один</description>
        <contentItemId>1</contentItemId>
      </magazineNumber>
      <magazineNumber>
        <title>Статья два</title>
        <description>Описание cтатьи два</description>
        <contentItemId>2</contentItemId>
      </magazineNumber>
      <magazineNumber>
        <title>Статья три</title>
        <description>Описание cтатьи три</description>
        <contentItemId>3</contentItemId>
      </magazineNumber>
    </items>
  </magazineNumber>
  <magazineNumber>
    <id>5</id>
    <title>Второй номер</title>
    <description>Описание второго номера</description>
    <items>
      <magazineNumber>
        <title>Статья один</title>
        <description>Описание cтатьи один</description>
        <contentItemId>4</contentItemId>
      </magazineNumber>
      <magazineNumber>
        <title>Статья два</title>
        <description>Описание cтатьи два</description>
        <contentItemId>5</contentItemId>
      </magazineNumber>
    </items>
  </magazineNumber>
</items>

并且嵌套列表显示扁平结构(所有项目同时 - 第1个顶级杂志编号,3个 - 儿童,第2个顶级杂志编号,3个孩子),但是如果我点击顶级项目(就xml文件数据)它显示3个项目的子项。如果点按子项 - 嵌套列表会返回“平面”视图。

我是否应该将自我关联模型与其他关联配置或嵌套模型一起使用,我应该将“项目”字段添加到模型或以某种方式更改store \ proxy配置吗?

非常感谢一些代码示例。

谢谢:)

1 个答案:

答案 0 :(得分:0)

你能试试吗

proxy: {
    type: 'ajax',
    url: 'http://localhost:57648/repo/dataMocks/contentListItems.xml',
    reader: {
       type: 'xml',
       record: 'magazineNumber'
    }
}

并进入store: { ..., autoLoad: true }

我希望这会有所帮助。 :) Ciao。