You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some communities call this Aspect-oriented programming. That wiki article is wordy, so here are some code examples from the Spring framework (Java). AOP "advice" is a way to have many functions run common code (another function) before or after they execute. This is easy in a language with first-class functions like JavaScript. Take one of our usual Controllers:
varauthenticationCheck=function(){if(!user.isAuthenticated()){redirect('/login');}};varHomeController=BaseController.extend({index: function(params,history){varmodel=newModel();returnthis.view(null,HomeView,model);}});// redefine HomeController#index to achieve "before" advicevaroriginalIndexMethod=HomeController.prototype.index;HomeController.prototype.index=function(){authenticationCheck();returnoriginalIndexMethod.apply(this,arguments);}
That isn't too useful as-is, but it makes more sense when you want to do an authentication check before every method, or just a small handful of them:
varBigController=BaseController.extend({index: function(params,history){/* big function body */},about: function(params,history){/* big function body */},contact: function(params,history){/* big function body */},admin: function(params,history){/* big function body */},journal: function(params,history){/* big function body */},stuffNoOneCanEverKnow: function(params,history){/* big function body */},secretsEveryoneKnowsAnyway: function(params,history){/* big function body */}});varauthenticationCheck=function(){if(!user.isAuthenticated()){redirect('/login');}};['admin','journal','stuffNoOneCanEverKnow'].forEach(function(name){varoriginalMethod=BigController.prototype[name];BigController.prototype[name]=function(){authenticationCheck();returnoriginalMethod.apply(this,arguments);};});
Brought up in the Lavaca meeting: example given was doing authentication before routes execute.
The text was updated successfully, but these errors were encountered: