javascript - link a var to an object property -
in file called controllers.js
, i've got these variables :
var indexctrl = require('./index'), offerctrl = require('./offer'), setlocalectrl = require('./setlocale'), specificationsctrl = require('./specifications');
and object :
var routes = { indexctrl: { 'fr-fr' : '/accueil', 'en-en' : '/home', 'de-de' : '/startseite' }, offerctrl: { 'fr-fr' : '/offre', 'en-en' : '/offer', 'de-de' : '/angebot' }, setlocalectrl: { 'fr-fr' : '/setlocale/:locale', 'en-en' : '/setlocale/:locale', 'de-de' : '/setlocale/:locale' }, specificationsctrl: { 'fr-fr' : '/specifications', 'en-en' : '/specifications-en', 'de-de' : '/spezifikationen' } };
for example, required index.js :
'use strict'; var indexmodel = require('../models/index'); exports.route = function(req, res, locale) { console.log(locale); var model = new indexmodel(); res.render('index', model); };
what can use indexctrl defined in var route
indexctrl defined above (with require) ?
edit : example :
for (var ctrl in routes) { var ctrlcontent = routes[ctrl]; (var language in ctrlcontent) { var route = ctrlcontent[language]; //router.get(route, ctrl); console.log('%s, %s, %s', ctrl, language, route); } }
the log displays :
indexctrl, fr-fr, /accueil indexctrl, en-en, /home indexctrl, de-de, /startseite offerctrl, fr-fr, /offre offerctrl, en-en, /offer offerctrl, de-de, /angebot setlocalectrl, fr-fr, /setlocale/:locale setlocalectrl, en-en, /setlocale/:locale setlocalectrl, de-de, /setlocale/:locale specificationsctrl, fr-fr, /specifications specificationsctrl, en-en, /specifications-en specificationsctrl, de-de, /spezifikationen
that way, know route /accueil
has rendered in locale fr-fr controller indexctrl. if try use ctrl.route
-for example indexctrl.route- defined in require, error : route.get() requires callback functions got [object undefined]
.
looking @ question, might trick:
var indexctrl = require(routes.indexctrl[language]), offerctrl = require(routes.offerctrl[language]), setlocalectrl = require(routes.setlocalectrl[language]), specificationsctrl = require(routes.specificationsctrl[language]);
Comments
Post a Comment