Changeset 9129

Show
Ignore:
Timestamp:
06/20/08 01:10:37 (4 months ago)
Author:
matthias
Message:

* Extended Filter module

  • enabled only and except options
  • added prepend methods
  • added Filter constructor, for internal use in the filter module
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/aida/demo-app/app/controllers/say_controller.js

    r9128 r9129  
    1313function SayController(req, res, session) { 
    1414    
    15    beforeFilter(authenticate); 
     15   function OutputCompressionFilter() { 
     16      this.filter = function() { 
     17         logger.info("###########"); 
     18      } 
     19   } 
     20    
     21   beforeFilter(authenticate, auth2, { except : ["index2", "foo", "indexx"] }); 
     22   beforeFilter(new OutputCompressionFilter()) 
    1623   afterFilter(afterFilter1); 
    1724   afterFilter(afterFilter2); 
     
    6976    
    7077   function authenticate() { 
     78      logger.info("---------xxxxx--------" + this.bla) 
    7179      return (req.data.action === "index"); 
    7280   } 
     81 
     82   function auth2() { 
     83      logger.info("---------x22xxxx--------") 
     84      return true; 
     85   } 
     86 
    7387    
    7488   function afterFilter1() { 
  • sandbox/aida/modules/aida/controller.js

    r9128 r9129  
    5555    
    5656   var content = ""; 
    57    if (!controller.beforeFilters.all()) return; 
    58    var content = controller.callAction(req.route.handler, req, res, session); 
    59    for (var i=0; i<controller.afterFilters.length; i++) { 
    60       content = controller.afterFilters[i].call(controller, content, req, res, session) || content; 
    61    } 
     57   if (!controller.beforeFiltersPass()) return; 
     58   content = controller.callAction(req.route.handler, req, res, session); 
     59   content = controller.applyAfterFilters(content, req, res, session); 
    6260   res.write(content); 
    6361} 
  • sandbox/aida/modules/aida/filters.js

    r9128 r9129  
    11 
    22/** 
    3  * 
     3 *  
    44 * 
    55 */ 
     6  
     7/** 
     8 * Constructor for filter object. 
     9 */ 
     10function Filter(filter, type, param) { 
     11   logger.info("1.filter:" + filter) 
     12   logger.info("2.filter:" + filter.filter) 
     13   this.filter = filter.filter ? filter.filter : filter; 
     14   this.param = param || {}; 
     15   if (this.param.only && !Object.isArray(this.param.only)) this.param.only = [this.param.only]; 
     16   if (this.param.except && !Object.isArray(this.param.except)) this.param.except = [this.param.except]; 
     17   this.type = type; 
     18} 
     19 
     20Filter.before = "before"; 
     21Filter.after  = "after"; 
     22 
     23 
     24/** 
     25 * Calls all before filters and returns true, if all filters return true, 
     26 * or if there are no before filters. 
     27 * @return {Boolean} 
     28 */ 
     29function beforeFiltersPass() { 
     30   for (var i=0; i<this.beforeFilters.length; i++) { 
     31      var f = this.beforeFilters[i]; 
     32      if (f.param.only && !f.param.only.include(req.route.action)) continue; 
     33      if (f.param.except && f.param.except.include(req.route.action)) continue; 
     34      if (f.filter.apply(this) === false) return false; 
     35   }    
     36   return true; 
     37} 
     38 
     39 
     40/** 
     41 * Calls all after filters and returns the manipulated content. 
     42 * @param {Object} controller   Controller instance 
     43 * @param {Object} req       Request object 
     44 * @param {Object} res       Response object 
     45 * @param {Object} session   Session object 
     46 * @return {String} 
     47 */ 
     48function applyAfterFilters(content, req, res, session) { 
     49   for (var i=0; i<this.afterFilters.length; i++) { 
     50      var f = this.afterFilters[i]; 
     51      if (f.param.only && !f.param.only.include(req.route.action)) continue; 
     52      if (f.param.except && f.param.except.include(req.route.action)) continue; 
     53      content = f.filter.call(this, content, req, res, session) || content; 
     54   }    
     55   return content; 
     56} 
     57 
     58 
     59/** 
     60 * @ignore 
     61 */ 
     62function createFilterObjects(args, type) { 
     63   var filters = $A(args); 
     64   var param = {}; 
     65   var last = filters[filters.length-1]; 
     66   if (!last.filter &&  !Object.isFunction(last)) { 
     67      param = filters.pop(); 
     68   } 
     69   filters = filters.collect(function(f) { 
     70      return new Filter(f, type, param) 
     71   }); 
     72   return filters; 
     73} 
     74  
    675 
    776/** 
     
    2291 * run before actions on this controller are performed. 
    2392 * @param {Function} filter*  A filter function which returns true, if the filter passed. 
    24  * @return {Array} beforeFilters 
    2593 * @see beforeFilter 
    2694 */ 
    2795function appendBeforeFilter() { 
    28    for (var i=0; i<arguments.length; i++) { 
    29       this.beforeFilters.push(arguments[i]); 
    30    } 
    31    return this.beforeFilters; 
     96   Array.prototype.push.apply( 
     97      this.beforeFilters,  
     98      createFilterObjects(arguments, Filter.before) 
     99   ); 
     100
     101 
     102 
     103/** 
     104 * The passed filters will be prepended to the array of filters that  
     105 * run before actions on this controller are performed. 
     106 * @param {Function} filter  A filter function which returns true, if the filter passed. 
     107 * @see beforeFilter 
     108 */ 
     109function prependBeforeFilter() { 
     110   Array.prototype.unshift.apply( 
     111      this.beforeFilters,  
     112      createFilterObjects(arguments, Filter.before) 
     113   ); 
    32114} 
    33115 
     
    49131 * The passed filters will be appended to the array of filters that  
    50132 * run after actions on this controller are performed. 
     133 * @param {Function} filter 
     134 *    A filter function which returns the filtered output string.  
     135 *    output, req, res, session will be passed as arguments to the filter function. 
     136 * @see afterFilter 
     137 */ 
     138function appendAfterFilter() { 
     139   Array.prototype.push.apply( 
     140      this.afterFilters,  
     141      createFilterObjects(arguments, Filter.after) 
     142   ); 
     143} 
     144 
     145 
     146/** 
     147 * The passed filters will be prepended to the array of filters that  
     148 * run after actions on this controller are performed. 
    51149 * @param {Function} filter*   
    52150 *    A filter function which returns the filtered output string.  
    53151 *    output, req, res, session will be passed as arguments to the filter function. 
    54  * @return {String} output  
    55152 * @see afterFilter 
    56153 */ 
    57 function appendAfterFilter() { 
    58    for (var i=0; i<arguments.length; i++) { 
    59       this.afterFilters.push(arguments[i]); 
    60    } 
    61    return this.afterFilters
     154function prependAfterFilter() { 
     155   Array.prototype.unshift.apply( 
     156      this.afterFilters,  
     157      createFilterObjects(arguments, Filter.after) 
     158   )
    62159} 
    63  
    64  
    65 /* 
    66 missing: 
    67 append_around_filter 
    68 around_filter       
    69 filter_chain    
    70 prepend_around_filter 
    71 skip_after_filter 
    72 skip_before_filter 
    73 skip_filter  
    74 */