From e4a3905c2bb613236fd932f3132e521343009c29 Mon Sep 17 00:00:00 2001 From: jlukic Date: Thu, 2 Oct 2014 14:10:10 -0400 Subject: [PATCH] Adds mutation observers, event delegation for accordion. Fixes callback context --- src/definitions/modules/accordion.js | 36 ++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/definitions/modules/accordion.js b/src/definitions/modules/accordion.js index f37b62bcd..b591162be 100755 --- a/src/definitions/modules/accordion.js +++ b/src/definitions/modules/accordion.js @@ -54,6 +54,7 @@ $.fn.accordion = function(parameters) { element = this, instance = $module.data(moduleNamespace), + observer, module ; @@ -61,9 +62,10 @@ $.fn.accordion = function(parameters) { initialize: function() { module.debug('Initializing accordion with bound events', $module); - $title - .on('click' + eventNamespace, module.event.click) + $module + .on('click' + eventNamespace, selector.title, module.event.click) ; + module.observeChanges(); module.instantiate(); }, @@ -84,6 +86,26 @@ $.fn.accordion = function(parameters) { ; }, + refresh: function() { + $title = $module.find(selector.title); + $content = $module.find(selector.content); + }, + + observeChanges: function() { + if(MutationObserver !== undefined) { + observer = new MutationObserver(function(mutations) { + module.debug('DOM tree modified, updating selector cache'); + module.refresh(); + }); + observer.observe(element, { + childList : true, + subtree : true + }); + module.debug('Setting up mutation observer', observer); + } + }, + + event: { click: function() { $.proxy(module.toggle, this)(); @@ -146,8 +168,8 @@ $.fn.accordion = function(parameters) { .addClass(className.active) ; $.proxy(module.reset.display, this)(); - $.proxy(settings.onOpen, element)(); - $.proxy(settings.onChange, element)(); + $.proxy(settings.onOpen, this)(); + $.proxy(settings.onChange, this)(); }) ; } @@ -180,8 +202,8 @@ $.fn.accordion = function(parameters) { .end() .slideUp(settings.duration, settings.easing, function() { $.proxy(module.reset.display, this)(); - $.proxy(settings.onClose, element)(); - $.proxy(settings.onChange, element)(); + $.proxy(settings.onClose, this)(); + $.proxy(settings.onChange, this)(); }) ; } @@ -435,6 +457,7 @@ $.fn.accordion = function(parameters) { }; $.fn.accordion.settings = { + name : 'Accordion', namespace : 'accordion', @@ -467,7 +490,6 @@ $.fn.accordion.settings = { content : '.content' } - }; // Adds easing