Created
September 5, 2013 03:38
-
-
Save particlebanana/6445791 to your computer and use it in GitHub Desktop.
An example of injecting dynamic routes for a language based app. See: https://github.com/balderdashy/sails/issues/821
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
// Add logic to /config/bootstrap.js for injecting routes | |
/** | |
* Bootstrap | |
* | |
* An asynchronous boostrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#documentation | |
*/ | |
// Require the Language Mapping | |
var routeMap = require('../api/services/languageMap').map; | |
module.exports.bootstrap = function (cb) { | |
// For each type of page bind all the language routes | |
Object.keys(routeMap).forEach(function(route) { | |
bindRoutes(routeMap[route]); | |
}); | |
/** | |
* Bind Routes For Page Type | |
* | |
* @param {Object} route | |
*/ | |
function bindRoutes(route) { | |
var controller = route.controller; | |
var action = route.action; | |
var controllerFn = sails.controllers[controller][action]; | |
// For each language route bind a route | |
for(var key in route.routes) { | |
sails.emit('router:bind', { | |
path : '/' + route.routes[key], | |
target : buildHandler(key, controllerFn), | |
verb : 'get' | |
}); | |
} | |
} | |
/** | |
* Build a Router Handler | |
* | |
* @param {String} langKey | |
* @param {Function} fn | |
*/ | |
function buildHandler(langKey, fn) { | |
return function(req, res, next) { | |
// Sets the languageType param to whatever the language key was | |
// for the route. Example: `de` | |
req.languageType = langKey; | |
// Call actual controller method | |
fn(req, res, next); | |
}; | |
} | |
// It's very important to trigger this callack method when you are finished | |
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap) | |
cb(); | |
}; |
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
// Place this in /api/services/languageMap.js | |
/** | |
* Maps Pages for various languages To URLs | |
*/ | |
exports.map = { | |
'electronics': { | |
controller: 'store', | |
action: 'typeA', | |
routes: { | |
en: 'electronics', | |
fr: 'electronique', | |
de: 'elektronik', | |
it: 'electronics' | |
} | |
}, | |
'books': { | |
controller: 'store', | |
action: 'typeB', | |
routes: { | |
en: 'books', | |
fr: 'livres', | |
de: 'bucher', | |
it: 'libri' | |
} | |
} | |
}; |
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
// An example controller from /api/controllers/StoreController.js | |
module.exports = { | |
typeA: function(req, res) { | |
res.send('typeA action with Lang Type: ' + req.languageType); | |
}, | |
typeB: function(req, res) { | |
res.send('typeB action with Lang Type: ' + req.languageType); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment