Created
September 17, 2013 14:39
-
-
Save rclark/6595226 to your computer and use it in GitHub Desktop.
class / function pattern for chaining
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
trailguide.models.segment('segment-2').fetch({ | |
success: function(segment, response, xhr) { | |
var butt = new trailguide.views.details({ | |
model: segment, | |
el: '#details' | |
}); | |
butt.render(); | |
} | |
}); | |
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
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