On Oct 26, 3:00 pm, Cliff <[EMAIL PROTECTED]> wrote:
> > $('btn1').observe('click', function() {
> > alert(123);
> > alert(321);
> > });
>
> That would be lovely, but all my methods live in a class. I need to
> pass variables to that class. Right now, I want to pass functions to
> that class. How can I do that? I only know how to pass functions
> inside a string, so I can only use the onclick thing.
>
> newbutton = Class.create({
> initialize: …
> setActions: function(desiredActions){
> //previously:
> identifier.writeAttribute('onclick', desiredActions);
>
> //preferred:
> $(this.id).observe('click', function() {
> ???
> }
> }
>
> });
I'm not sure I understand exactly what you mean. If you want to pass a
set of functions to be executed when click event occurs, then it's
quite trivial:
...
setActions: function() {
var desiredActions = $A(arguments);
if (!desiredActions.length) return;
$(this.id).observe('click', function(event) {
desiredActions.each(funtion(action) {
action.call(this, event);
})
})
}
...
(new newbutton()).setActions(
function(){ alert(123); },
function(){ alert(321); }
);
While you can do something like this, it probably isn't the best
option. I don't see a reason to create multiple function objects when
one could suffice:
(new newbutton()).setActions(function(){
alert(123);
alert(321);
});
and define `setActions` method like so:
...
setActions: function(handler) {
$(this.id).observe('click', handler);
}
...
Passing one function simplifies things quite noticeably, as you can
see.
P.S. I would also suggest capitalizing "class" names - for convention
(i.e. `Button` rather than `newbutton`).
--
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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---