On Oct 27, 3:11 am, Cliff <[EMAIL PROTECTED]> wrote:
> Kangax: this works like a charm.> (new Button()).setActions( function(){
> >   alert(123);
> >   alert(321);
> > } );
>
> > and define `setActions` method like so:
> > ...
> > setActions: function(handler) {
> >   $(this.id).observe('click', handler);}
> > ...
>
> This is very nice, what would we do without experts such as yourself!
> I've managed to make it work.
> One more question: I'd like to make it easy for my team to setActions
> for the new button class. If I use the renovated method as above, I
> have to write an extra function(){ } code to contain the actions I
> want to pass to the button. Is there any way to simplify the process?

I think passing a function is the most straight-forward and simple way
to attach behavior to a button. Functions are first class objects in
JS and are very much suited for things like that. They are also parsed
much faster from function objects rather than strings (as in `eval`,
Function constructor and `setTimeout`/`setInterval`).

If there's a predefined set of actions that can be attached to a
button, you could try associating those actions with class names. It
would then be easy for, say, designers to change buttons' behaviors
without messing with the code:

var Button = Class.create({
  initialize: function(id){
    this.element = $(id);
    this.setActions();
  },
  setActions: function(){
     if (this.element.hasClassName('alertable')) {
        this.element.observe('click', function(){ alert(123); });
     }
     if (this.element.hasClassName('highlightable')) {
        this.element.observe('click', function()
{ this.highlight(); });
     }
  }
});

> --
> Cliff

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@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-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to