Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 23rd, 2012  |  syntax: None  |  size: 2.12 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <cfscript>
  2.  
  3.  
  4.         // I return a new object with only the given method names expsed
  5.         // as behaviors on the resultant object.
  6.         function exposeMethods( target, methods ){
  7.  
  8.                 // Define the behavior that will be exposed. This method will
  9.                 // act as a proxy to the underlying behavior which will pass
  10.                 // the message onto the target component and return the value
  11.                 // returned from the target component.
  12.                 var behavior = function(){
  13.  
  14.                         // Get the name of the function being invoked.
  15.                         var methodName = getFunctionCalledName();
  16.  
  17.                         // Invoke the method on the taret object and return the
  18.                         // result to the calling context.
  19.                         return(
  20.                                 invoke( target, methodName, arguments )
  21.                         );
  22.  
  23.                 };
  24.  
  25.                 // Create the object we are going to return.
  26.                 var proxy = {};
  27.  
  28.                 // For each method name, expose the behavior through the
  29.                 // proxy object.
  30.                 arrayEach(
  31.                         methods,
  32.                         function( methodName ){
  33.  
  34.                                 // Expose the behavior.
  35.                                 proxy[ methodName ] = behavior;
  36.  
  37.                         }
  38.                 );
  39.  
  40.                 // Return the new proxy object with exposed behaviors.
  41.                 return( proxy );
  42.  
  43.         }
  44.  
  45.  
  46.         // ------------------------------------------------------ //
  47.         // ------------------------------------------------------ //
  48.         // ------------------------------------------------------ //
  49.         // ------------------------------------------------------ //
  50.  
  51.  
  52.         // Create a new account component instance.
  53.         account = new Account( 100 );
  54.  
  55.  
  56.         // Now, let's create a proxy to the given account component that
  57.         // only exposes the credit and the balance methods but not the
  58.         // debit method.
  59.         goodAccount = exposeMethods(
  60.                 account,
  61.                 [ "credit", "getBalance" ]
  62.         );
  63.  
  64.  
  65.         writeOutput( "Account credited [50]. <br />" );
  66.  
  67.         // Credit the account.
  68.         goodAccount.credit( 50 );
  69.  
  70.  
  71.         // Now, I know this is going to fail, but for funzies, let's try
  72.         // to call the non-exposed behavior - debit() - on the proxy.
  73.         try {
  74.  
  75.                 writeOutput( "Trying to debit()." );
  76.  
  77.                 // Try to invoke the debit() method - a non-exposed behavior.
  78.                 goodAccount.debit( 25 );
  79.  
  80.         } catch( Any cfcatch ){
  81.  
  82.                 writeOutput( " [ FAILED ] <br />" );
  83.  
  84.         }
  85.  
  86.  
  87.         // Now, let's call the exposed behavior to get the balanace.
  88.         writeOutput( "Balanace: " & goodAccount.getBalance() );
  89.  
  90.  
  91. </cfscript>