Last active
July 20, 2016 18:30
-
-
Save zenithtekla/c5271f1b56cafb5259202b592419a9f2 to your computer and use it in GitHub Desktop.
prototypal programming
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
/* | |
var productCategoryRouteConf = { | |
app: '', | |
routeTable: [], | |
// later all we add these | |
init : function(){}, | |
addRoutes: function(){} | |
} | |
*/ | |
// constructor fn | |
function productCategoryRouteConf(app){ | |
this.app = app; | |
this.routeTable = []; | |
this.init(); // method initialize | |
} | |
// purpose is to first populate our route | |
productCategoryRouteConf.prototype.init = function(){ | |
var self = this; // ref to this obj | |
this.addRoutes(); | |
this.processRoutes(); | |
} | |
// populate route tables app.get('/', routes.index); app.get('/about', routes.about); app.get('/contact', routes.contact); | |
productCategoryRouteConf.prototype.addRoutes = function(){ | |
var self = this; | |
self.routeTable.push({ | |
requestType: 'get', | |
requestUrl: '/createProductCategory', | |
cbFunc: function(req, res){ | |
// try to render the createProductCategory page if it exists. | |
res.render('createProductCategory', { title: 'Create Product Category' }); | |
} | |
}); | |
} | |
productCategoryRouteConf.prototype.processRoutes = function(){ | |
var self = this; | |
self.routeTable.forEach(function(route){ | |
// all routes stated in here | |
if(route.requestType == 'get'){ | |
self.app.get(route.requestUrl, route.cbFunc); | |
} | |
else if (route.requestType == 'post'){} | |
else if (route.requestType == 'delete'){} | |
}); | |
} | |
// exporting the constructor obj. | |
module.exports = productCategoryRouteConf; | |
/* | |
in app.js | |
var productCategoryRoute = require('./routes/productCategoryRouteConf.js'); | |
// that obj is available and it is a constructor function, so passing app as parameter to the constructor, app is an Express obj | |
new productCategoryRoute(app); | |
source: Shiva https://www.youtube.com/watch?v=m9vHvOAR3nQ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment