Andreas Bachmann wrote:
> Hi
>
> I'm new to prototype and have some problems creating a class...
> really!!
> I'm using script.aculo.us too, but it's not a problem... rightnow ;)
>
> My code:
>
> var ImageButton = Class.create({
>     initialize: function(e) {
>         this.e = e;
>         this.e.observe('mouseover', this.onMousOver);
>         this.e.observe('mouseout', this.onMousOut);
>     },
>
>     onMousOver: function() {
>         this.fadeIn();
>     },
>
>     onMouseOut: function() {
>         this.fadeOut();
>     },
>
>     fadeIn: function() {
>         new Effect.Fade(e, { duration: 1 });
>     },
>
>     fadeOut: function() {
>         new Effect.Appear(e, { duration: 1, from: 1.0, to: 1.0 });
>     }
> });
>
> document.observe('dom:loaded' , function() {
>     var previous = new ImageButton($('previous'));
>     var next    = new ImageButton($('next'));
> });
>
> --> Firebug result: this.fadeIn is not a function
>
> The same code but shorter:
>
> var ImageButton = Class.create({
>     initialize: function(e) {
>         this.e = e;
>         this.e.observe('mouseover', this.fadeIn);
>         this.e.observe('mouseout', this.fadeOut);
>     },
>
>     fadeIn: function() {
>         new Effect.Fade(e, { duration: 1 });
>     },
>
>     fadeOut: function() {
>         new Effect.Appear(e, { duration: 1, from: 1.0, to: 1.0 });
>     }
> });
>
> document.observe('dom:loaded' , function() {
>     var previous = new ImageButton($('previous'));
>     var next    = new ImageButton($('next'));
> });
>
> --> Firebug result: e is not defined
>
> Any advice to get to work?
>
>   
On the first bit, you need to bind the functions to "this" since the 
body of those functions reference "this". so:

    initialize: function(e) {
        this.e = e;
        this.e.observe('mouseover', this.onMousOver.bind(this));
        this.e.observe('mouseout', this.onMousOut.bind(this));
    },



On the second bit, you forgot to put "e" as a parameter to fadeIn() and 
fadeOut().

- Ken Snyder

--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to