Git Product home page Git Product logo

Comments (1)

zship avatar zship commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.