Skip to content

Instantly share code, notes, and snippets.

@rclark
Created September 17, 2013 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rclark/6595226 to your computer and use it in GitHub Desktop.
Save rclark/6595226 to your computer and use it in GitHub Desktop.
class / function pattern for chaining
trailguide.models.segment('segment-2').fetch({
success: function(segment, response, xhr) {
var butt = new trailguide.views.details({
model: segment,
el: '#details'
});
butt.render();
}
});
trailguide.models.Segment = Backbone.Model.extend({
urlRoot: '/db/segments/',
parse: function (response, options) {
var additional = {};
additional.geometry = response.geometry;
additional.projectedGeometry = trailguide.geoUtils.project(response.geometry);
additional.jstsGeometry = trailguide.geoUtils.geojson2jsts(additional.projectedGeometry);
return _.extend(additional, response.properties);
},
toJSON: function () {
var keysToRemove = [
'geometry',
'projectedGeometry',
'jstsGeometry',
'leafletLayer'
];
return {
type: "Feature",
properties: _.omit(this.attributes, keysToRemove),
geometry: this.get('geometry')
};
},
getDistance: function () {
return this.get('jstsGeometry').getLength();
},
getEndpoints: function () {
var coords = this.get('geometry').coordinates;
endpoints = [coords[0], coords[coords.length - 1]];
return endpoints;
},
getAdjacentTrailheads: function () {
// get any trailheads corresponding with
// this segment's endpoints
},
getAdjacentSegments: function () {
// get any other segments corresponding with
// this segment's endpoints
var url = 'db/segments/_design/geo/_view/endpoints?keys=' + this.getEndpoints();
},
getPOIs: function() {
// get POIs associated with this segment
var poiIDs = this.get('pois');
},
getRelatedRoutes: function () {
// get any routes that include this segment
},
leafletLayer: function (options) {
options = options || {};
return L.geoJson(this.toJSON(), options);
},
details: function () {
// prepare the details object for
// this segment's view
var details = {
'distance': this.getDistance()
};
var pois = this.get('pois');
if (pois) {
details.pois = pois;
}
var accessibility = this.get('accessibility');
if (accessibility) {
details.accessibility = accessibility;
}
return { 'details': details };
}
});
trailguide.models.segment = function(segmentId) {
return new trailguide.models.Segment({id: segmentId});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment