0
votes

I am using Sencha Touch 1.1 to build a bar locator. I am using CakePHP as my backend API and I want to be able to use the data as Cake outputs it.

Viewing the JSON data below you will see that the Pub data is returned in the 'Pub' array and the Suburb in a similar way. So accessing the data would be done as follows:

Pub.id, Pub.name, Pub.address_1

Does anyone know how I can use this format in my Sencha model and store?

I have my model and store setup as follows:

Ext.regModel('Pub', {
    fields: ['id', 'name', 'address_1', 'address_2', 'marker', 'lat', 'lng', 'suburb']
});

Ext.regStore('NearbyStore', {
    model: 'Pub',
    sorters: 'suburb',
    getGroupString: function(record) {
        return record.get('suburb');
    },
    proxy: {
        type: 'scripttag',
        url: 'http://exampleurl/bars/nearby.json?lat=-55.8874&lng=-11.177',
        reader: {
            type: 'json',
            root: 'results'
        }
    },
    autoLoad: true
});

Below is the JSON data that is returned from my store proxy.

   stcCallback1001({"results":[{"Pub":{"id":"125","name":"Brownsville Bowling & Recreation Club","address_1":"31a malouffst","address_2":"-","marker":"default_marker.png","lat":"-33.887402","lng":"151.177002"},"Suburb":{"name":"Knoxville"},"0":{"distance":"0.0002511751890598611"}},{"Pub":{"id":"1721","name":"Hampshire Hotel","address_1":"91 parramatta rd","address_2":"-","marker":"default_marker.png","lat":"-33.886799","lng":"151.177002"},"Suburb":{"name":"Brownsville"},"0":{"distance":"0.06684402352323478"}}]});
2

2 Answers

0
votes

you should see http://docs.sencha.com/touch/1-1/#!/api/Ext.data.Field-cfg-mapping

similar question on stackoverflow : Accessing nested objects in JSON feed - Sencha Touch

So you should map your model like this :

Ext.regModel('Pub', {
fields: [
{   
    name: 'id',
    type: 'string',
    mapping: 'Pub.id'
},
{
    name: 'name'
    type: 'string',
    mapping: 'Pub.name'
}, 
{
    name: 'address_1',
    type: 'string',
    mapping: 'Pub.address_1'
}, 
{
    name: 'AND SO ON.......'
}
});

Well, I've just started on sencha touch. So I hope im helping.. :)

0
votes

you will not only have to make these, but also map filtering, remote sorting, success properties, validation errors, and so on. If you are planning a bigger project I would recommend you to switch to Sencha Touch 2 and use Bancha.

Then you will not have to do any of those things. And as a nice bonus you can simply expose your CakePHP models and don't have to write them manually again for Sencha Touch.