我對sencha touch很陌生,我試圖解析一個數據數組(這似乎不是一個不常見的用例,但我在網上找不到任何關於它的任何信息)。我遵循嵌套json上的sencha ext.data.reader.json文檔,但它不起作用。這裏是我的模型: 搜索結果(持有多個搜索結果):Sencha Touch 2數組解析
Ext.define('GS.model.SearchResults', {
extend: 'Ext.data.Model',
autoLoad: true,
config: {
fields: [
{name: 'query', type: 'string'},
],
hasMany : {model: 'SearchResult', name: 'results'},
}
});
和搜索結果,舉行一個單獨的搜索結果
Ext.define('GS.model.SearchResult', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'}
],
belongsTo: 'SearchResults'
}
});
然後在我的控制,我有這樣的代碼:
var store = Ext.create('Ext.data.Store', {
autoLoad: "true",
model: "GS.model.SearchResults",
proxy:
{
type: 'ajax',
url : 'www.someurl.com/?query=somequery',
reader: {
type: 'json'
}
}
});
store.load({
callback: function() {
console.log("Done Loading");
var root = store.first();
console.log("Results for " + root.get('query')); //this prints correctly
console.log(root.results());//THIS IS THE LINE IM INTERESTED IN
console.log(root.raw.results);//this weirdly works
//now I want to print each search results name
root.results().each(function(result) {
console.log("Song: " + result.get('name'));
});
}
});
}
當我登錄root.results(),我得到
Uncaught TypeError: Object [object Object] has no method 'results'
這正是他們在文檔中的做法,所以有人知道爲什麼這不是工作?
編輯:這是我following
您確定您需要一個單獨的模型定義只是爲了包含多個SearchResult嗎?爲什麼不將SearchResults存儲在數組中? –
這對我有意義,但這不是他們如何做他們的例子。我只是添加了鏈接到問題btw – Jameo
此外,我打算進一步深入到這個heirarchy後,我只是想要得到這個簡單的例子工作 – Jameo