[Prototype-core] Re: A callbacks module for adding before/after hooks to object methods

2008-05-16 Thread Florian Traverse
You can have a look at how it is handled in Archetype Components (an Open
Source Framework based on Prototype). It's called MethodBuilder, but this is
basically a Facet in AOP.

It does not just add a before and an after for any function of a component,
but it also provides some really enjoyable tools like having a this bound
to the component in all it's function, so you don't have to put
.bind(this) everywhere when using callbacks.

Get more informations at http://archetypejs.org :)

2008/5/16 kangax [EMAIL PROTECTED]:


 I have done something similar in Class.addBehavior:
 http://github.com/kangax/protolicious/tree/master/class.addbehavior.js
 On the other hand, it's actually pretty easy to do this with plain
 Function#wrap:

 (function(p){
  p.push = p.push.wrap(function(){
var args = $A(arguments), proceed = args.shift();
console.log('before: ' + args);
(function(){ console.log('after: ' + args) }).defer();
return proceed.apply(proceed, args);
  })
 })(Array.prototype);

 It's arguably not as clean as your implementation, so some could
 definitely find it useful : )

 - kangax

 On May 14, 12:13 pm, Pat Nakajima [EMAIL PROTECTED] wrote:
  I've started putting together a way to specify per-object before/after
  callbacks for any method. Probably not something to go in core, but
  maybe something that would be interesting to core contributors.
 
  There are some examples in a GitHub wiki (http://github.com/nakajima/
  nakatype/wikis/callbacks), but basically, you specify callbacks like
  so:
 
var someArray = [1,2,3];
 
Callbacks.add(someArray, {
  before: {
push: function(entry) {
  var msg = 'about to push ' + entry + ' on array: ' +
  this.join(', ');
  console.info(msg);
}
  },
 
  after: {
push: function(entry) {
  var msg = 'just pushed ' + entry + ' on array: ' +
  this.join(', ');
  console.info(msg);
}
  }
});
 
someArray.push(4);
 
  In addition to adding 4 to someArray, the above code snippet will log
  to the console before and after.
 
  If anybody has any suggestions/criticisms/threats, please do share!
 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: A callbacks module for adding before/after hooks to object methods

2008-05-15 Thread Richard Quadling

2008/5/14 Pat Nakajima [EMAIL PROTECTED]:

 I've started putting together a way to specify per-object before/after
 callbacks for any method. Probably not something to go in core, but
 maybe something that would be interesting to core contributors.

 There are some examples in a GitHub wiki (http://github.com/nakajima/
 nakatype/wikis/callbacks), but basically, you specify callbacks like
 so:

  var someArray = [1,2,3];

  Callbacks.add(someArray, {
before: {
  push: function(entry) {
var msg = 'about to push ' + entry + ' on array: ' +
 this.join(', ');
console.info(msg);
  }
},

after: {
  push: function(entry) {
var msg = 'just pushed ' + entry + ' on array: ' +
 this.join(', ');
console.info(msg);
  }
}
  });

  someArray.push(4);

 In addition to adding 4 to someArray, the above code snippet will log
 to the console before and after.

 If anybody has any suggestions/criticisms/threats, please do share!

A very useful idea.

How far is it from simple before/after callbacks to a full AOP implementation?

Where I can see this being of instant use is the ability to develop a
class with no logging/diagnostics from day 1 and have a drop-in
before/after logger which wouldn't upset the main class.

Rather than having to name the methods, it would be great to be able
to wildcard/regex them ( /[sg]et.+/ for example) as well as allowing
all with an exclude mechanism (allow the high use methods to NOT be
logged).

I used to do Delphi development for a Point of Sale system. One of the
issues that we had was the user would say the calculation was wrong.
By using a plugin we could effectively record all the values of all
the calculations and the source of the values and produce a report of
any calculation. Remove the plugin and the app did nothing. All the
hooking and recording was in the plugin. This was useful especially
when you are converting costs with markups for bulk quantities
converted between buying and selling units.

It is in keeping with the unobtrusive nature of much JS code.

I think the callback handler should be available as an extension (like
the deprecated.js one is). So, officially supported/recognised, but
not built-in as standard.

Maybe amend the rake dist process to allow those with the skills to
build their own prototype.js with the required extensions built in.





-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: A callbacks module for adding before/after hooks to object methods

2008-05-15 Thread Pat Nakajima

With regards to your desire to skip the callbacks for some methods,
you can actually call methodWithoutCallbacks to call a method sans-
callbacks.

I think that the wildcard/regex idea is neat, though I'm not sure how
it could be accomplished unobtrusively. I could imagine overriding all
of the object's methods to be called through a sort of method handler
(similar to what I was playing with when I created this lazy class:
http://github.com/nakajima/nakatype/tree/master/src/lazy_class.js),
then having the method handler match callbacks to called methods. It
seems a bit cleaner than my example, though it doesn't quite feel
right still.

I'm actually not so familiar with AOP, so perhaps after I spend some
time with that, I'll be able to tackle some of these ideas.

On May 15, 8:17 am, Richard Quadling [EMAIL PROTECTED]
wrote:
 2008/5/14 Pat Nakajima [EMAIL PROTECTED]:





  I've started putting together a way to specify per-object before/after
  callbacks for any method. Probably not something to go in core, but
  maybe something that would be interesting to core contributors.

  There are some examples in a GitHub wiki (http://github.com/nakajima/
  nakatype/wikis/callbacks), but basically, you specify callbacks like
  so:

   var someArray = [1,2,3];

   Callbacks.add(someArray, {
     before: {
       push: function(entry) {
         var msg = 'about to push ' + entry + ' on array: ' +
  this.join(', ');
         console.info(msg);
       }
     },

     after: {
       push: function(entry) {
         var msg = 'just pushed ' + entry + ' on array: ' +
  this.join(', ');
         console.info(msg);
       }
     }
   });

   someArray.push(4);

  In addition to adding 4 to someArray, the above code snippet will log
  to the console before and after.

  If anybody has any suggestions/criticisms/threats, please do share!

 A very useful idea.

 How far is it from simple before/after callbacks to a full AOP implementation?

 Where I can see this being of instant use is the ability to develop a
 class with no logging/diagnostics from day 1 and have a drop-in
 before/after logger which wouldn't upset the main class.

 Rather than having to name the methods, it would be great to be able
 to wildcard/regex them ( /[sg]et.+/ for example) as well as allowing
 all with an exclude mechanism (allow the high use methods to NOT be
 logged).

 I used to do Delphi development for a Point of Sale system. One of the
 issues that we had was the user would say the calculation was wrong.
 By using a plugin we could effectively record all the values of all
 the calculations and the source of the values and produce a report of
 any calculation. Remove the plugin and the app did nothing. All the
 hooking and recording was in the plugin. This was useful especially
 when you are converting costs with markups for bulk quantities
 converted between buying and selling units.

 It is in keeping with the unobtrusive nature of much JS code.

 I think the callback handler should be available as an extension (like
 the deprecated.js one is). So, officially supported/recognised, but
 not built-in as standard.

 Maybe amend the rake dist process to allow those with the skills to
 build their own prototype.js with the required extensions built in.

 --
 -
 Richard Quadling
 Zend Certified Engineer :http://zend.com/zce.php?c=ZEND002498r=213474731
 Standing on the shoulders of some very clever giants!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---



[Prototype-core] Re: A callbacks module for adding before/after hooks to object methods

2008-05-15 Thread Richard Quadling

2008/5/15 Pat Nakajima [EMAIL PROTECTED]:

 With regards to your desire to skip the callbacks for some methods,
 you can actually call methodWithoutCallbacks to call a method sans-
 callbacks.

 I think that the wildcard/regex idea is neat, though I'm not sure how
 it could be accomplished unobtrusively. I could imagine overriding all
 of the object's methods to be called through a sort of method handler
 (similar to what I was playing with when I created this lazy class:
 http://github.com/nakajima/nakatype/tree/master/src/lazy_class.js),
 then having the method handler match callbacks to called methods. It
 seems a bit cleaner than my example, though it doesn't quite feel
 right still.


This is top of my head stuff.

// Allow the wrapping of many different methods.
Callback.addWrappers : function(klass_ToWrap, before/after/around,
{array of methods}/single method/regex, klass_Payload.Payload)

// Allow the wrapping of all methods except those supplied.
Callback.addWrappersAllBut : function(klass_ToWrap,
before/after/around, {array of methods}/single method/regex,
klass_Payload.Payload)

Param 1 : klass_ToWrap is the class to which we are going to wrap some
additional behaviour.

Param 2 : before/after/around is 1 of the three ways to wrap the
behaviour : before only, after only or around which is both before and
after.

Param 3 : {array of methods}/single method/regex : allow an array of
known methods to be wrapped, a single method or a regex

Param 4 : the method to call. The first argument in the callback will
be to indicate if it is before or after. This allows for a single
method to be wrapped around another. The other parameters need to be
the parameters passed to the klass_ToWrap method.


I would guess that there would be a private method which did the
wrapping for a single source class method. So both Callback methods
iterate the list of supplied methods and call the private method to
do the wrapping.


So, in the JS file that defines the logging class (which would need to
be loaded after prototype.js, the callback handling class and the
klass_being_wrapped), ...

var klass_Logging = Class.create({

  // Use Firebug's console to show trace.
  Logging : function(b_Before, s_Klass, s_Method) {
   switch(b_Before) {
case true :
 console.group('Entering ' + s_Klass + '.' + s_Method + '(',
arguments, ')');
 break;
default :
 console.groupEnd();
   }
  }
});

// Add Firebug tracing to klass_ToWrap
Callback.addWrappers(klass_ToWrap, 'around', /*/, klass_Logging.Logging);



 I'm actually not so familiar with AOP, so perhaps after I spend some
 time with that, I'll be able to tackle some of these ideas.

 On May 15, 8:17 am, Richard Quadling [EMAIL PROTECTED]
 wrote:
 2008/5/14 Pat Nakajima [EMAIL PROTECTED]:
  I've started putting together a way to specify per-object before/after
  callbacks for any method. Probably not something to go in core, but
  maybe something that would be interesting to core contributors.

  There are some examples in a GitHub wiki (http://github.com/nakajima/
  nakatype/wikis/callbacks), but basically, you specify callbacks like
  so:

   var someArray = [1,2,3];

   Callbacks.add(someArray, {
 before: {
   push: function(entry) {
 var msg = 'about to push ' + entry + ' on array: ' +
  this.join(', ');
 console.info(msg);
   }
 },

 after: {
   push: function(entry) {
 var msg = 'just pushed ' + entry + ' on array: ' +
  this.join(', ');
 console.info(msg);
   }
 }
   });

   someArray.push(4);

  In addition to adding 4 to someArray, the above code snippet will log
  to the console before and after.

  If anybody has any suggestions/criticisms/threats, please do share!

 A very useful idea.

 How far is it from simple before/after callbacks to a full AOP 
 implementation?

 Where I can see this being of instant use is the ability to develop a
 class with no logging/diagnostics from day 1 and have a drop-in
 before/after logger which wouldn't upset the main class.

 Rather than having to name the methods, it would be great to be able
 to wildcard/regex them ( /[sg]et.+/ for example) as well as allowing
 all with an exclude mechanism (allow the high use methods to NOT be
 logged).

 I used to do Delphi development for a Point of Sale system. One of the
 issues that we had was the user would say the calculation was wrong.
 By using a plugin we could effectively record all the values of all
 the calculations and the source of the values and produce a report of
 any calculation. Remove the plugin and the app did nothing. All the
 hooking and recording was in the plugin. This was useful especially
 when you are converting costs with markups for bulk quantities
 converted between buying and selling units.

 It is in keeping with the unobtrusive nature of much JS code.

 I think the callback handler should be available as an extension (like
 the deprecated.js one is). So, officially 

[Prototype-core] Re: A callbacks module for adding before/after hooks to object methods

2008-05-15 Thread kangax

I have done something similar in Class.addBehavior:
http://github.com/kangax/protolicious/tree/master/class.addbehavior.js
On the other hand, it's actually pretty easy to do this with plain
Function#wrap:

(function(p){
  p.push = p.push.wrap(function(){
var args = $A(arguments), proceed = args.shift();
console.log('before: ' + args);
(function(){ console.log('after: ' + args) }).defer();
return proceed.apply(proceed, args);
  })
})(Array.prototype);

It's arguably not as clean as your implementation, so some could
definitely find it useful : )

- kangax

On May 14, 12:13 pm, Pat Nakajima [EMAIL PROTECTED] wrote:
 I've started putting together a way to specify per-object before/after
 callbacks for any method. Probably not something to go in core, but
 maybe something that would be interesting to core contributors.

 There are some examples in a GitHub wiki (http://github.com/nakajima/
 nakatype/wikis/callbacks), but basically, you specify callbacks like
 so:

   var someArray = [1,2,3];

   Callbacks.add(someArray, {
 before: {
   push: function(entry) {
 var msg = 'about to push ' + entry + ' on array: ' +
 this.join(', ');
 console.info(msg);
   }
 },

 after: {
   push: function(entry) {
 var msg = 'just pushed ' + entry + ' on array: ' +
 this.join(', ');
 console.info(msg);
   }
 }
   });

   someArray.push(4);

 In addition to adding 4 to someArray, the above code snippet will log
 to the console before and after.

 If anybody has any suggestions/criticisms/threats, please do share!
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---