0
votes

I'm new to sencha touch and I'm trying to parse an array of data (this doesn't seem like an uncommon use case but I can't find anything about it online). I followed the sencha ext.data.reader.json doc on nested json, but it doesn't work. Here are my models: Search Results (to hold multiple search results):

Ext.define('GS.model.SearchResults', {
extend: 'Ext.data.Model',
autoLoad: true,
config: {
    fields: [
        {name: 'query', type: 'string'},
    ],
    hasMany  : {model: 'SearchResult', name: 'results'},

        }
});

And search result, to hold an individual search result

Ext.define('GS.model.SearchResult', {
extend: 'Ext.data.Model',

config: {
    fields: [
        {name: 'id',     type: 'int'},
        {name: 'name',      type: 'string'}
    ],
    belongsTo: 'SearchResults'
}
});

Then in my controller, I have this code:

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'));
            });

        }
    });

}

When I log root.results(), I get

Uncaught TypeError: Object [object Object] has no method 'results'

This is exactly how they do it in the docs, so does anyone know why this isnt working???

Edit: Here is the doc I was following

2
Are you sure you need a separate model definition just to contain more than one SearchResult? Why not just store your SearchResults in an array?Daniel Miladinov
That makes sense to me but that isn't how they do their examples. I just added the link to the question btwJameo
Also I plan to drill further down into this heirarchy later, I'm just trying to get this simple example workingJameo

2 Answers

0
votes

The best way to check your error is to debug in chrome console. After callback you will find your records in root.raw or root.data.

I use to debug in chrome console and write the required code.

0
votes

After some painful trial and error, I figured it out. In the tutorial, they used unconventional model name, but in my case I needed to use the fully qualified one

So to fix I needed to change

hasMany  : {model: 'SearchResult', name: 'results'},

to

hasMany  : {model: 'GS.model.SearchResult', name: 'results'},

and same with my deeper model like this:

belongsTo: 'GS.model.SearchResults'

Tough error to catch, but I hope this can help someone else in my position