this.myMethod() and this.setOptions() are not working!

215 views
Skip to first unread message

Douglas Machado

unread,
Sep 12, 2012, 4:06:24 PM9/12/12
to mootool...@googlegroups.com
Greetings,

I'm having a hard time to make a method to work correctly and I appreciate if anyone could help me out;

I have uploaded the script to this JSFiddle:

It is not executing this method below
this.hideAll(i);

Also, if I uncomment this.setOptions() I get this error in Firebug's console:
TypeError: this.setOptions is not a function

Thanks in advanced for your help,

rasmusfl0e

unread,
Sep 12, 2012, 4:58:10 PM9/12/12
to mootool...@googlegroups.com
If you use the this keyword in events you'll most likely need to bind this to the event function - like so:

heading.addEvent('click', function(){
  // yada yada
  this.hideAll(i);
  // yada yada
}.bind(this));


It's basically also what's giving you that TypeError when you try to fire Slider.initialize on domready

... But - that part it superflous; MooTools Classes automatically call their initialize method on creation. Which means you're actually firing it twice! - so no need to have it run on domready.

what you should be doing is something like:

var Slider; // to define Slider in the global scope
window.addEvent('domready', function () {
    Slider = new MooFAQ();
   
Slider.slide();
    Slider.loadHash();
}
);

idan Hen

unread,
Sep 12, 2012, 5:08:46 PM9/12/12
to mootool...@googlegroups.com
Hey !!

i had the same problem , but in your code : 
heading.addEvent('click'function(){
  // yada yada
  this.hideAll(i); -- 'this' is not the class , but rather the button !!
  // yada yada
}); 

to solve it , you have a few options : 
1.    use that variable .

// saving the this in a variable before we create the callback function .
var that = this;
heading.addEvent('click'function(){
  // yada yada
  that.hideAll(i); // now that is know in the function , and its basicly your callback 'this'
  // yada yada
}); 

2. if you do something with the 'this' - you can use the call function .

var that = this;
heading.addEvent('click'function(e){
  // use the this which is the button if needed.
    this.do_something_of_button();
  that.hideAll.call(that,e); -- the hideAll function will run , and inside it the 'this' is the that you gave in the first argument ! 
  // yada yada
}); 

Douglas Machado

unread,
Sep 12, 2012, 6:09:07 PM9/12/12
to mootool...@googlegroups.com
@rasmusfl0e,

Thanks for your reply, however it did not work;

Also, I have tried the code below, but it did not work:

var Slider; // to define Slider in the global scope
window.addEvent('domready', function () {
    Slider = new MooFAQ();
   
Slider.slide();
    Slider.loadHash();
}
);


I don't know whats wrong with my class, but it did not working using bind(this) and it is not calling the initialize() method;

I've updated the jsfiddle with your suggestions:
http://jsfiddle.net/machadoug/LPVkv/2/

Thanks for helping.

Douglas Machado

unread,
Sep 12, 2012, 6:10:26 PM9/12/12
to mootool...@googlegroups.com
@idan Hen,

Thanks for your reply, however it did not work either;

I've updated the jsfiddle with your suggestions. Please take a look:
http://jsfiddle.net/machadoug/LPVkv/3/

Thanks for helping.

rasmusfl0e

unread,
Sep 13, 2012, 7:58:05 AM9/13/12
to mootool...@googlegroups.com
You're missing a bind on the each loop in the slide method: 

this
.headings.eachfunction(headingiel{
    // yada yada
}.bind(this)); 

g.schlmm

unread,
Sep 13, 2012, 10:04:12 AM9/13/12
to mootool...@googlegroups.com
There is a bind paramater for each,some,filter, etc...:

this.headings.each( function(heading, i, el) {
// yada yada
}, this);

Douglas Machado

unread,
Sep 13, 2012, 11:25:12 AM9/13/12
to mootool...@googlegroups.com
Many thanks to all for your help.

It is now Working! ;-)

If anyone is looking for this answer in the future:
I had to add .bind(this) and I had to actually add the options to the constructor, which I thought was optional;

All the best,

Rolf Langenhuijzen

unread,
Sep 17, 2012, 2:37:31 PM9/17/12
to mootool...@googlegroups.com
Page does not exist(?) solved?

Douglas Machado -Focalizar.com.br

unread,
Sep 17, 2012, 2:38:28 PM9/17/12
to mootool...@googlegroups.com
Yes, it is solved! ;-)

Thanks.
Reply all
Reply to author
Forward
0 new messages