Decorate not working? #16

Closed
loveencounterflow opened this Issue Jan 23, 2012 · 3 comments

Comments

Projects
None yet
4 participants

when trying to make decorators work, i produced the following code:

var log = console.log;

var Logged = function( method ){
  return new Compose.Decorator( function( method_name ) {
    log( "decorating " + method_name );
    this[ method_name ] = function() {
      log( "entering " + method_name );
      R = method.apply( this, arguments );
      log( "leaving " + method_name );
      return R;
    }
  });
};

var C = Compose( {
  'frob': Logged( function() { log( "performing" + frob ) } ) } );
c = new C()
c.frob()

which, i believe, keeps pretty close to the example given in the documentation. however, running the above gives

 Error: Decorator not applied

is this a bug or is my own code buggy?

I just pasted the example from the README into Chrome's console and got the same error. I don't think Compose.Decorator was ever written for use outside of it's own internals. It'd be nice to hear from someone who worked on it whether or not that is the case @kriszyp?

Contributor

neonstalwart commented Aug 14, 2012

i made a working version of your example at http://jsfiddle.net/neonstalwart/Bdhpe/. i think this is a bug with compose. when there is a single argument passed to compose it doesn't call mixin for that arg and mixin is where the decorators are applied.

the workaround (maybe it's how it's supposed to be used?) is to provide an object as the first argument

var C = Compose({}, {
  'frob': Logged( function(frob) { console.log( "performing " + frob ) } ) 
} );
Owner

kriszyp commented Aug 15, 2012

Decorators are intended to provide the logic when one method overrides another. However, you can provide a second argument that is a function that is executed if no overrides take place. A simple logging decorator can easily be created without a decorator, so I altered the documentation to give a better example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment