【问题标题】:How to implement hierarchical model in Sencha Touch如何在 Sencha Touch 中实现层次模型
【发布时间】:2011-11-30 18:36:57
【问题描述】:

我已经阅读了很多问题/答案和教程,但我仍然无法在 Sencha Touch 中实现分层模型。根模型是标签。每个标签都有许多项目。这是我的模型:

根标签:

app.models.Tag = Ext.regModel("Tag", {
    fields : [ {
        name : "id",
        type : "int"
    }, {
        name : "name",
        type : "string"
    }], 
    associations : {
        type : "hasMany",
        model : "TagItem", 
        name : "items"
    }
});

添加标签项

app.models.TagItem = Ext.regModel("TagItem", {
    fields : [ {
        name : "id",
        type : "int"
    }, {
        name : "title",
        type : "string"
    }, {
        name : "type", 
        type : "string"
    }, {
        name : "tag_id", 
        type : "int"
    }], 
    associations : {
        belongsTo : {
            model : "Tag",
            name : "items"
        }
    }
});

标签存储:

app.stores.tagItems = new Ext.data.Store({
    model : "app.models.TagItem",
    proxy : {
        type : 'scripttag',
        root : 'items',
        url : 'http://www.s-lab.cz/ios/api/tags.php'
    }
});

标签:

app.stores.tags = new Ext.data.Store({
    model : "app.models.TagItems",
    proxy : {
        type : 'scripttag',
        url : 'http://www.s-lab.cz/ios/api/tags.php'
    }
});

当我尝试从商店加载商品时,我得到:

未捕获的类型错误:无法调用未定义的“读取”方法

我的 JSON 如下所示:

stcCallback1005([
 {
    "id":1,
    "name":"Doktor",
    "url":"doktor",
    "items":[
       {
          "type":"video",
          "id":1,
          "title":"First tag item",
          "tag_id":1
       },
       {
          "type":"article",
          "id":1,
          "title":"Second tag item - article",
          "tag_id":1
       },
       {
          "type":"article",
          "id":2,
          "title":"Third tag item - article",
          "tag_id":1
       },
       {
          "type":"video",
          "id":2,
          "title":"Fourth tag item",
          "tag_id":1
       }
    ]
 },
 {
    "id":2,
    "name":"Nemocnice",
    "url":"nemocnice"
 },
 {
    "id":3,
    "name":"Sestra",
    "url":"sestra"
     }
  ]);

更新:

我用建议更改了代码,结果 JSON 是这样的:

stcCallback1005({
    "results": [{
        "id": 1,
        "name": "Doktor",
        "url": "doktor",
        "items": [{
            "type": "video",
            "id": 1,
            "title": "First tag item",
            "tag_id": 1
        }, {
            "type": "article",
            "id": 1,
            "title": "Second tag item - article",
            "tag_id": 1
        }, {
            "type": "article",
            "id": 2,
            "title": "Third tag item - article",
            "tag_id": 1
        }, {
            "type": "video",
            "id": 2,
            "title": "Fourth tag item",
            "tag_id": 1
        }]
    }, {
        "id": 2,
        "name": "Nemocnice",
        "url": "nemocnice"
    }, {
        "id": 3,
        "name": "Sestra",
        "url": "sestra"
    }]
});

但我仍然无法访问后续项目:app.stores.tags.getAt(0) 有效,但 app.stores.tags.getAt(0).TagItems() 无效(app.stores.tags.getAt(0).itemsapp.stores.tags.getAt(0).items() 无效)。而且我的模板也没有正确渲染:<tpl for=".">{name}<ul><tpl for="items"><li>{title} ({type})</li></tpl></ul></tpl>

有什么想法吗?

【问题讨论】:

    标签: javascript model sencha-touch


    【解决方案1】:

    我认为您不需要为模型注册分配变量。

    Ext.regModel("Tag", {
        fields : [ {
            name : "id",
            type : "int"
        }, {
            name : "name",
            type : "string"
        }], 
        associations : {
            type : "hasMany",
            model : "TagItem", 
            name : "items"
        }
    });
    

    所以;

    Ext.regModel("TagItem", {
        fields : [ {
            name : "id",
            type : "int"
        }, {
            name : "title",
            type : "string"
        }, {
            name : "type", 
            type : "string"
        }, {
            name : "tag_id", 
            type : "int"
        }], 
        associations : {
            belongsTo : {
                model : "Tag",
                name : "items"
            }
        }
    });
    

    将阅读器添加到 json 并更正注册的模型名称。

    标签存储:

    app.stores.tagItems = new Ext.data.Store({
        model : "TagItem", //Use registered name
        proxy : {
            type : 'scripttag',
            url : 'http://www.s-lab.cz/ios/api/tags.php'
    
                       reader: { //add reader to read from json
                          type: 'json',
                          root: 'results'
                       }
        }
    });
    

    标签:

    app.stores.tags = new Ext.data.Store({
        model : "Tag", //Use registered name
        proxy : {
            type : 'scripttag',
            url : 'http://www.s-lab.cz/ios/api/tags.php'
                       reader: { //add reader to read from json
                          type: 'json',
                         root: 'results'
                       }
        }
    });
    

    最后也许您需要将 tags.php 中的 json 格式更改为;

    {"results":[
     {
        "id":1,
        "name":"Doktor",
        "url":"doktor",
        "items":[
           {
              "type":"video",
              "id":1,
              "title":"First tag item",
              "tag_id":1
           },
           {
              "type":"article",
              "id":1,
              "title":"Second tag item - article",
              "tag_id":1
           },
           {
              "type":"article",
              "id":2,
              "title":"Third tag item - article",
              "tag_id":1
           },
           {
              "type":"video",
              "id":2,
              "title":"Fourth tag item",
              "tag_id":1
           }
        ]
     },
     {
        "id":2,
        "name":"Nemocnice",
        "url":"nemocnice"
     },
     {
        "id":3,
        "name":"Sestra",
        "url":"sestra"
         }
      ]
    }
    

    我希望它有效或有助于更正您的代码。

    【讨论】:

    • 感谢您的回答。不幸的是,它没有帮助。查看更新的问题。
    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多