So4ne
So4ne

Reputation: 1182

Complicated nested json loops to store with extjs

I'm using a geojson extracted from naturalearthdata which looks like that : enter image description here

All I want is to catch the NAME of each feature in order to display them in a grid (live search grid.. BTW is it efficient for 2000 names?) But I can't access to all the name with root property. I tried to loop into all the features

Ext.define('myApp.store.Places', {
    extend: 'Ext.data.Store',
    alias: 'store.places',
    requires : ['myApp.model.PlacesModel',
                'myApp.view.main.MainModel'],
    id: 'Places',
    model: 'myApp.model.PlacesModel',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url : '/resources/data/coord.json',
        reader: {
            type: 'json',
            transform: {
                fn: function(data) {
                    for(var i = 0; i < data.features.length -1; i++){
                        names_places.push(data.features[i].properties.NAME);
                    }
                    debugger;
                    return names_places;
                },
                scope: this
            }
        }
    }
});

But the debugger sent me that result which I don't understand : enter image description here

Especially when the array looks good : enter image description here

What is the good way to catch only the NAME? Does the return has to look to a json?

Upvotes: 2

Views: 926

Answers (1)

weeksdev
weeksdev

Reputation: 4355

You can use the mapping attribute on the fields array in your model definition to map the correct attribute in the json to a field.

You set the rootProperty to features for the reader.

Then in your fields array something similar to this

fields: [
    { name: 'myCustomField', mapping: 'properties.NAME' }
]

Upvotes: 2

Related Questions