Comments (1)
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:
var HomeController = BaseController.extend({
index: function(params, history) {
var model = new Model();
return this.view(null, HomeView, model);
}
});
Add an authentication check the "regular" way:
var authenticationCheck = function() {
if (!user.isAuthenticated()) {
redirect('/login');
}
};
var HomeController = BaseController.extend({
index: function(params, history) {
authenticationCheck();
var model = new Model();
return this.view(null, HomeView, model);
}
});
Do the same thing AOP-style:
var authenticationCheck = function() {
if (!user.isAuthenticated()) {
redirect('/login');
}
};
var HomeController = BaseController.extend({
index: function(params, history) {
var model = new Model();
return this.view(null, HomeView, model);
}
});
// redefine HomeController#index to achieve "before" advice
var originalIndexMethod = HomeController.prototype.index;
HomeController.prototype.index = function() {
authenticationCheck();
return originalIndexMethod.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:
var BigController = 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 */ }
});
var authenticationCheck = function() {
if (!user.isAuthenticated()) {
redirect('/login');
}
};
['admin', 'journal', 'stuffNoOneCanEverKnow'].forEach(function(name) {
var originalMethod = BigController.prototype[name];
BigController.prototype[name] = function() {
authenticationCheck();
return originalMethod.apply(this, arguments);
};
});
AOP has its supporters and detractors just like anything else. The Wikipedia article talks a little bit about the pros and cons.
from lavaca.
Related Issues (20)
- Remove mvc/Application#ajax HOT 3
- Simplify routing by removing mvc/Controller
- Remove util/Config (and then util/Map) HOT 3
- Replace util/StringUtils with mout/string HOT 2
- Remove mvc/PageView HOT 1
- Replace usages of util/Cache with plain objects HOT 2
- Make View#mapEvent (and applyEvents) extensible HOT 1
- Investigate supporting Web Components/Custom Elements
- Investigate possibly replacing mvc/Widget with mvc/View HOT 1
- EventDispatcher is not disposing of object bound events HOT 5
- Remove thisp from EventDispatcher.js HOT 1
- Add ability to use external files in src/mvc/View#mapEvent HOT 2
- 'rendersuccess' needs to be triggered when template is rendered and in the DOM HOT 1
- lavaca/net/History: navigating to a specific URL by changing the window.location.hash does not work HOT 1
- lavaca/env/Device.init: allow multiple registrations (like $(document).ready)
- Convert build process to webpack HOT 1
- Unit tests failing HOT 5
- Remove bower in favor of npm HOT 1
- Remove references to `thisp` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lavaca.