I have a working javascript solution for a submenu, where the submenu container is not a child or sibling of the link container. I'm just trying to experiment with the prototype pattern. So I am trying to translate the already working code into a prototype pattern to get a more readable code and I'm currently failing. I get no errors in the console, but the code does not work.
jQuery(document).ready(function ($) {
function tg_dropdown(element) {
this.element = element;
this.mainNavigation = $('#top-menu');
this.dropdownLink = this.mainNavigation.find('.has_dropdown');
this.bindEvents();
}
tg_dropdown.prototype.bindEvents = function() {
var $tg_megamenu__link = $('.tg-megamenu__link'),
self = this;
$tg_megamenu__link.mouseenter(function(event) {
self.showDropdown();
}).mouseleave(function() {
setTimeout( function () {
if ($('#top-menu').find('.has-dropdown:hover').length == 0 && $('#main-header').find('.tg-submenu:hover').length == 0)
self.hideDropdown();
}, 50);
})
};
tg_dropdown.prototype.showDropdown = function () {
$('body').addClass('test');
};
tg_dropdown.prototype.hideDropdown = function () {
$('body').removeClass('test');
}
});
I might oversee some fundamental basics of the prototype pattern. Can somebody direct me into the right way to get this thing working?