[英]How to implement hierarchical model in Sencha Touch
我已經閱讀了許多問題/答案和教程,但仍然無法在Sencha Touch中實現分層模型。 根模型是Tag。 每個標簽都有許多項目。 這是我的模型:
根標簽:
app.models.Tag = Ext.regModel("Tag", {
fields : [ {
name : "id",
type : "int"
}, {
name : "name",
type : "string"
}],
associations : {
type : "hasMany",
model : "TagItem",
name : "items"
}
});
插入的TagItem
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'
}
});
當我嘗試從商店中加載商品時,我得到:
未捕獲的TypeError:無法調用未定義的方法“讀取”
我的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).items
都app.stores.tags.getAt(0).items
或app.stores.tags.getAt(0).items()
可以)。 而且我的模板無法正確呈現: <tpl for=".">{name}<ul><tpl for="items"><li>{title} ({type})</li></tpl></ul></tpl>
任何想法?
我認為您無需為模型注冊分配變量。
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"
}
]
}
我希望它可以工作或有助於更正您的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.