application.js
function callAction(request, response, callback) {
    if (typeof callback == 'function') {
      return Promive.resolve()
        .then(callback.bind(null, request, response));
    }

    try {
      var Action = require(ACTION_PATH + '/' + callback.action + '.js');
    } catch (error) {
      if (error.message == 'Cannot find module \'' + ACTION_PATH + '/' + callback.action + '.js\'') {
        return Promise.reject('Action ' + callback.action + ' does not exist.')
          .catch(this.onPageNotFound.bind(this, request, response));
      }
      return Promise.reject(error)
        .catch(this.onError.bind(this, request, response));
    }

    var instance = new Action(request, response);
    // Test if method exists
    if (!instance[callback.method]) {
      return Promise.reject(new Error('Method "' + callback.method + '" not found in action "' + callback.action + '"'));
    }

    var promise = Promise.resolve();
    if (instance.init && 'function' == typeof instance.init) {
      promise = promise.then(function () {
        return instance.init();
      });
    }

    promise = promise.then(function () {
      return instance[callback.method].apply(instance, callback.arguments);
    });

    if (instance.onError && 'function' == typeof instance.onError) {
      promise = promise.catch(function (error) {
        return instance.onError.call(instance, error)
      });
    }

    promise = promise.catch(this.onError.bind(this, request, response));

    return promise;
  }
function callAction(request, response, callback) {
    if (typeof callback == 'function') {
      return Promive.resolve()
        .then(callback.bind(null, request, response));
    }

    try {
      var Action = require(ACTION_PATH + '/' + callback.action + '.js');
    } catch (error) {
      if (error.message == 'Cannot find module \'' + ACTION_PATH + '/' + callback.action + '.js\'') {
        return Promise.reject('Action ' + callback.action + ' does not exist.')
          .catch(this.onPageNotFound.bind(this, request, response));
      }
      return Promise.reject(error)
        .catch(this.onError.bind(this, request, response));
    }

    var instance = new Action(request, response);
    // Test if method exists
    if (!instance[callback.method]) {
      return Promise.reject(new Error('Method "' + callback.method + '" not found in action "' + callback.action + '"'));
    }

    var promise = Promise.resolve();
    if (instance.init && 'function' == typeof instance.init) {
      promise = promise.then(function () {
        return instance.init();
      });
    }

    promise = promise.then(function () {
      return instance[callback.method].apply(instance, callback.arguments);
    });

    if (instance.onError && 'function' == typeof instance.onError) {
      promise = promise.catch(function (error) {
        return instance.onError.call(instance, error)
      });
    }

    promise = promise.catch(this.onError.bind(this, request, response));

    return promise;
  }
function callAction ( request , response , callback ) { if ( typeof callback == 'function' ) { return Promive . resolve ( ) . then ( callback . bind ( null , request , response ) ) ; } try { var Action = require ( ACTION_PATH + '/' + callback . action + '.js' ) ; } catch ( error ) { if ( error . message == 'Cannot find module \'' + ACTION_PATH + '/' + callback . action + '.js\'' ) { return Promise . reject ( 'Action ' + callback . action + ' does not exist.' ) . catch ( this . onPageNotFound . bind ( this , request , response ) ) ; } return Promise . reject ( error ) . catch ( this . onError . bind ( this , request , response ) ) ; } var instance = new Action ( request , response ) ; // Test if method exists if ( ! instance [ callback . method ] ) { return Promise . reject ( new Error ( 'Method "' + callback . method + '" not found in action "' + callback . action + '"' ) ) ; } var promise = Promise . resolve ( ) ; if ( instance . init && 'function' == typeof instance . init ) { promise = promise . then ( function ( ) { return instance . init ( ) ; } ) ; } promise = promise . then ( function ( ) { return instance [ callback . method ] . apply ( instance , callback . arguments ) ; } ) ; if ( instance . onError && 'function' == typeof instance . onError ) { promise = promise . catch ( function ( error ) { return instance . onError . call ( instance , error ) } ) ; } promise = promise . catch ( this . onError . bind ( this , request , response ) ) ; return promise ; }
Call action and method form resolved route
@param request
@param response
@param callback
@returns {*}
Call action and method form resolved route