使用hasMany将嵌套的GeoJson加载到FeatureStore。可能?

时间:2014-10-21 08:10:26

标签: extjs openlayers geoext

通过vectorLayer将嵌套的GeoJson加载到FeatureStore时是否可以使用关联?

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

因此,第一个jsonStore按预期工作,employeesStore会为每个公司填充。第二个geoJsonStore没有。员工数据保留在raw.data中,子库不会在加载时填充。

它应该以这种方式工作,还是我错过了什么?

这是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" }
                    ]
                }
            }
        ]
    }

1 个答案:

答案 0 :(得分:0)

看来,最简单的方法是在加载功能后重写数据,例如“featuresadded”事件:

    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();
        }
    );
},