Last active
December 14, 2015 16:19
-
-
Save cspanring/5114078 to your computer and use it in GitHub Desktop.
Dabbling with GeoJSON and ember.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The GeoJSON object | |
// | |
// { | |
// "type": "FeatureCollection", | |
// "features": [ | |
// { | |
// "type": "Feature", | |
// "geometry": { | |
// "type": "Point", | |
// "coordinates": [42.3875, -71.1] | |
// }, | |
// "properties": { | |
// "prop0": "value0" | |
// } | |
// } | |
// ] | |
// } | |
// ...as ember.js model | |
App.Adapter = DS.RESTAdapter.extend(); | |
App.Store = DS.Store.extend({ | |
revision: 12, | |
adapter: 'App.Adapter' | |
}); | |
App.FeatureCollection = DS.Model.extend({ | |
type: DS.attr('string'), | |
features: DS.hasMany('App.Feature') | |
}); | |
App.Adapter.map('App.FeatureCollection', { | |
features: { embedded: 'always' } | |
}); | |
App.Feature = DS.Model.extend({ | |
type: DS.attr('string'), | |
featurecollection: DS.belongsTo('App.FeatureCollection'), | |
geometry: DS.belongsTo('App.Geometry'), | |
properties: DS.belongsTo('App.Properties') | |
}); | |
App.Adapter.map('App.Feature', { | |
geometry: { embedded: 'always' }, | |
properties: { embedded: 'always' } | |
}); | |
// a point | |
DS.RESTAdapter.registerTransform('point', { | |
serialize: function(value) { | |
return [value.get('x'), value.get('y')]; | |
}, | |
deserialize: function(value) { | |
return Ember.create({ x: value[1], y: value[0] }); | |
} | |
}); | |
App.Geometry = DS.Model.extend({ | |
feature: DS.belongsTo('App.Feature'), | |
type: DS.attr('string'), | |
coordinates: DS.attr('point') | |
}); | |
App.Properties = DS.Model.extend({ | |
feature: DS.belongsTo('App.Feature'), | |
prop0: DS.attr('string') | |
}); | |
// ...should behave like... | |
var feature = App.Feature.find(1); | |
feature.get('geometry.coordinates.x'); | |
feature.get('geometry.coordinates.y'); | |
feature.get('properties.prop0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From what I can see, this only supports Point Geometries, like on the provided example.
LineString geometries have an array of points as coordinates. Trying to implement this using ember-data models.