I'm new to javascript/jQuery.
I am doing a simple fade slider to use on a page, and I'm trying to put as much as I can inside the object, so it works out of the box whenever I create a new instance of the object. While setting up the navigation, I first did the following:
testimonial = new slider($('.testimonials'), $('.testimonials-nav'));
testimonial.nav.on('click', function (e) {
testimonial.setCurrent($(this).data('dir')); // changes the current slider
testimonial.transition(); // what happens when the 'current' changes
testimonial.setHeight(); //because everything is 'position: absolute', I set the parent div height with js
e.preventDefault();
});
But that way I'd need to repeat all this code everytime I create a new instance of the slider, so I tried a different approach, I created two methods:
slider.prototype.init = function () {
this.numberEls();
this.setHeight();
this.activateNav();
};
so when I create a new instance I can do the following
testimonial = new slider($('.testimonials'), $('.testimonials-nav'));
testimonial.init();
and created this method
slider.prototype.activateNav = function () {
this.nav.on('click', function (e) {
this.setCurrent($(this).data('dir'));
e.preventDefault();
});
};
but it doesn't work obviously because of the scope of the "this" keyword inside the event handler, how do I make the "this.setCurrent" work in this context?
Another question, overall, is it a good approach for what I'm trying to accomplish? remember that it might be used in other parts of the site, not only in testimonials.
And if it's not too much to ask, how could I make it loops automatically? I know it involves setTimeOut() or something, but I don't know exactly how (I need to be honest and say it's a lazy question, I actually didn't even try to do it before asking how, but since I am here =P)
PS. I'm sorry if I didn't make myself clear, english is not my first language and I struggle with it sometimes =P