Hello,
I want to overwrite the default-behaviour of the prototype-function
"addClassName". How can I achieve this? A few weeks ago I saw a piece
of code that overwrites the "wrap"-function - just take a look here:
http://pastie.caboo.se/137552
I tried the same with "addClassName", but nothing happend:
Object.extend(Array.prototype, {
	addClassName: function(element, className) {
		alert(''test'');
	}
});
Thanks for any help!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 23, 2008 7:31 AM, ohneworte <alexander.gewessler-Tswl7xcH0yE@public.gmane.org> wrote:> I tried the same with "addClassName", but nothing happend: > Object.extend(Array.prototype, { > addClassName: function(element, className) { > alert(''test''); > } > });You are adding the method "addClassName" onto the Array class. That is not going to do what you are expecting. Change Array.prototype to Element.prototype. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
ohneworte wrote:> Hello, > > I want to overwrite the default-behaviour of the prototype-function > "addClassName". How can I achieve this? A few weeks ago I saw a piece > of code that overwrites the "wrap"-function - just take a look here: > http://pastie.caboo.se/137552 > > I tried the same with "addClassName", but nothing happend: > Object.extend(Array.prototype, { > addClassName: function(element, className) { > alert(''test''); > } > }); > > Thanks for any help! >You''ll want to use Element.addMethods() - see http://prototypejs.org/api/element/addMethods - Ken Snyder Element.addMethods({ addClassName: function(element, className) { alert(''test''); } }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I think you are close with your code snippet, but rather than
extending Function.wrap(), I think what you want to do is use
Function.wrap() to over-write Element.addClassName() like so:
Element.prototype.addClassName = Element.prototype.addClassName.wrap(
    // the "proceed argument in refs the orriginal
"addClassName", the
other arguments are the original args to addClassName
    // (you could leave these out and accessed using the arguments
array. in this way, you could add your own new arguemtns to the method
call)
    function(proceed, element, className ){
        if (/* logic to determine whether you want call your
overridden method or the original (often based on argument list) */)
        {
             // do your overridden thing here ... //
        }
        else /* if not the overridden method, call the original
*/
            return proceed();
    }
)
see here for a great explanation:
http://thinkweb2.com/projects/prototype/2007/09/14/wrap-it-up/
the api documentation: http://www.prototypejs.org/api/function/wrap
Ned
On Jan 23, 10:10 am, Ken Snyder
<kendsny...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> ohneworte wrote:
> > Hello,
>
> > I want to overwrite the default-behaviour of the prototype-function
> > "addClassName". How can I achieve this? A few weeks ago I
saw a piece
> > of code that overwrites the "wrap"-function - just take a
look here:
> >http://pastie.caboo.se/137552
>
> > I tried the same with "addClassName", but nothing happend:
> > Object.extend(Array.prototype, {
> >    addClassName: function(element, className) {
> >            alert(''test'');
> >    }
> > });
>
> > Thanks for any help!
>
> You''ll want to use Element.addMethods() -
seehttp://prototypejs.org/api/element/addMethods
>
> - Ken Snyder
>
> Element.addMethods({
>   addClassName: function(element, className) {
>     alert(''test'');
>   }
>
> });
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---