简体   繁体   中英

Loading nested GeoJson to FeatureStore with hasMany. Possible?

Is it possible to use associations while loading nested GeoJson to FeatureStore via vectorLayer?

    Ext.define('Employee', {
        extend: 'Ext.data.Model',
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                idProperty: 'id'
            }
        },
        fields: [ { name: 'name', type: 'string' } ]
    });

    Ext.define('Company', {
        extend: 'Ext.data.Model',
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                idProperty: 'id'
            }
        },
        fields: [ { name: 'name', type: 'string' } ],
        hasMany: { model: 'Employee', name: 'employees' }
    });

    var jsonData = {
        companies: [
            {
                name: 'Foo',
                employees: [
                    {   name: 'Jack' },
                    {   name: 'Joe' }
                ]
            },
            {
                name: 'Bar',
                employees: [
                    {   name: 'Jim' }
                ]
            }
        ]
    };

    Ext.define('CompaniesExt', {
        extend: 'Ext.data.Store',
        model: 'Company',
        data: jsonData,
        storeId: 'CompaniesExt',
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'companies'
            }
        }
    });

    Ext.define('CompaniesGeoExt', {
        extend: 'GeoExt.data.FeatureStore',
        model: 'Company',
        storeId: 'CompaniesGeoExt'
    });

    // data from json
    var jsonStore = Ext.create('CompaniesExt');

    // data from geoJson
    var map = new OpenLayers.Map({ allOverlays: true });
    var geoJsonStore = Ext.create('CompaniesGeoExt');
    var layer = new OpenLayers.Layer.Vector('Companies', { 
        storeName: 'CompaniesGeoExt',
        strategies: [
            new OpenLayers.Strategy.Fixed(), 
        ],          
        protocol: new OpenLayers.Protocol.HTTP({
            url: "/companies.geojson",
            format: new OpenLayers.Format.GeoJSON(),
        })
    });
    map.addLayers([layer]);
    geoJsonStore.bind(layer);

So, the first jsonStore works as expected, the employeesStore gets populated for each company. The second geoJsonStore does not. Employees data remain in raw.data and the sub-stores don't ge populated on load.

Is it supposed to work this way, or am I missing something?

Here's the contents of companies.geojson:

{
        "type": "FeatureCollection", 
        "features": [
            {
                "geometry": {
                    "type": "point",
                    "coordinates": [ 0, 0 ]
                },
                "type": "feature",
                "properties": {
                    "name": "Foo",
                    "employees": [
                        {   "name": "Jack" },
                        {   "name": "Joe" }
                    ]
                }
            },
            {
                "geometry": {
                    "type": "point",
                    "coordinates": [ 1, 1 ]
                },
                "type": "feature",
                "properties": {
                    "name": "Bar",
                    "employees": [
                        {   "name": "Jim" }
                    ]
                }
            }
        ]
    }

It seems, that the easiest way is to rewrite data after loading features, for example on "featuresadded" event:

    rewriteEmployees: function(event){

      // which store
      var store = event.features[0].layer.store; 

      // for each item do the rewrite
      store.each(
        function(r){
          if (r.raw.data.emplyees)
                r.raw.data.employees.forEach(function(e){
                    r.employees().add(e);
                });
                r.employees().commitChanges();
        }
    );
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM