Last active
January 1, 2016 06:39
-
-
Save craigspaeth/8106908 to your computer and use it in GitHub Desktop.
Notes on how routing code might be shared b/t Backbone and Express
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
# Pretending we have a module called rooter.. | |
# Option #1 - Abstract only the shared portion | |
# | |
# Accepts a callback and the params. The callback takes (err, locals). | |
# Rooter then injects locals into the express request, and when mixed into | |
# Backbone it wraps the route handlers passing the locals as the first argument. | |
# This splits the further handling of the routing to the respective server/client | |
# routing engines. | |
# lib/setup/rooter.coffee | |
rooter '/artwork/:id', (callback, id) -> | |
new Artwork(id: id).fetch().then callback | |
# server/routes.coffee | |
app.get '/artwork/:id', (req, res) -> | |
res.render 'artwork-page' | |
# client/router.coffee | |
Router extends Backbone.Router | |
routes: | |
'/artwork/:id': 'artwork' | |
artwork: (locals, id) -> | |
$('body').html require('artwork-detail')(locals) | |
# Option #2 - Combined routing | |
# | |
# This joins the routing code server/client together potentially replacing both | |
# express and Backbone's routers. It provies callbacks that get excuted either | |
# on the client or server. This means the routing code would live together, | |
# allowing any shared routing code to execute outside of the callbacks. | |
rooter '/artwork/:id', (client, server, id) -> | |
new Artwork(id: id).fetch().then (err, artwork) -> | |
client -> | |
return alert('fail') if err | |
$('body').html require('artwork-detail')(artwork: artwork) | |
server (req, res, next) -> | |
return next(err) if err | |
res.render 'artwork-page' if req.method is 'GET' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment