From c5d08cac83fb67b625f88bfb34ca56ac80cef9e6 Mon Sep 17 00:00:00 2001 From: Chris Oyler Date: Thu, 17 Dec 2015 14:46:01 -0800 Subject: [PATCH] pretty big update to how foundation manages initialized plugins. from the outside, everything works the same, internally, no longer storing a complete copy of every plugin instantiation on the foundation object, instead, only storing uuids, also improved the check for plugin double dipping and (I think) fixed the issue where multiple calls to $(document).foundation() *could* cause multiple plugins from being registered on a single element --- js/foundation.core.js | 74 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/js/foundation.core.js b/js/foundation.core.js index 6bcac1aef..565347813 100644 --- a/js/foundation.core.js +++ b/js/foundation.core.js @@ -53,19 +53,17 @@ var Foundation = { */ registerPlugin: function(plugin, name){ var pluginName = name ? hyphenate(name) : functionName(plugin.constructor).toLowerCase(); - plugin.uuid = this.GetYoDigits(6, pluginName); - if(!plugin.$element.attr('data-' + pluginName)){ - plugin.$element.attr('data-' + pluginName, plugin.uuid); - } + if(!plugin.$element.attr('data-' + pluginName)){ plugin.$element.attr('data-' + pluginName, plugin.uuid); } + if(!plugin.$element.data('zfPlugin')){ plugin.$element.data('zfPlugin', plugin); } /** * Fires when the plugin has initialized. * @event Plugin#init */ plugin.$element.trigger('init.zf.' + pluginName); - this._activePlugins[plugin.uuid] = plugin; + this._uuids.push(plugin.uuid); return; }, @@ -77,16 +75,16 @@ var Foundation = { * @fires Plugin#destroyed */ unregisterPlugin: function(plugin){ - var pluginName = functionName(plugin.constructor).toLowerCase(); + var pluginName = hyphenate(functionName(plugin.$element.data('zfPlugin').constructor)); - delete this._activePlugins[plugin.uuid]; - plugin.$element.removeAttr('data-' + pluginName).removeData('zf-plugin') + this._uuids.splice(this._uuids.indexOf(plugin.uuid), 1); + plugin.$element.removeAttr('data-' + pluginName).removeData('zfPlugin') /** * Fires when the plugin has been destroyed. * @event Plugin#destroyed */ .trigger('destroyed.zf.' + pluginName); - for(prop in plugin){ + for(var prop in plugin){ plugin[prop] = null;//clean up script to prep for garbage collection. } return; @@ -98,33 +96,33 @@ var Foundation = { * @param {String} plugins - optional string of an individual plugin key, attained by calling `$(element).data('pluginName')`, or string of a plugin class i.e. `'dropdown'` * @default If no argument is passed, reflow all currently active plugins. */ - _reflow: function(plugins){ - var actvPlugins = Object.keys(this._activePlugins); - var _this = this; - - if(!plugins){ - actvPlugins.forEach(function(p){ - _this._activePlugins[p]._init(); - }); - - }else if(typeof plugins === 'string'){ - var namespace = plugins.split('-')[1]; - - if(namespace){ - - this._activePlugins[plugins]._init(); - - }else{ - namespace = new RegExp(plugins, 'i'); - - actvPlugins.filter(function(p){ - return namespace.test(p); - }).forEach(function(p){ - _this._activePlugins[p]._init(); - }); - } - } - + reInit: function(plugins){ + // var actvPlugins = Object.keys(this._activePlugins); + // var _this = this; + // + // if(!plugins){ + // actvPlugins.forEach(function(p){ + // _this._activePlugins[p]._init(); + // }); + // + // }else if(typeof plugins === 'string'){ + // var namespace = plugins.split('-')[1]; + // + // if(namespace){ + // + // this._activePlugins[plugins]._init(); + // + // }else{ + // namespace = new RegExp(plugins, 'i'); + // + // actvPlugins.filter(function(p){ + // return namespace.test(p); + // }).forEach(function(p){ + // _this._activePlugins[p]._init(); + // }); + // } + // } + // }, /** @@ -170,7 +168,7 @@ var Foundation = { var $el = $(this), opts = {}; // Don't double-dip on plugins - if ($el.data('zf-plugin')) { + if ($el.data('zfPlugin')) { console.warn("Tried to initialize "+name+" on an element that already has a Foundation plugin."); return; } @@ -182,7 +180,7 @@ var Foundation = { }); } try{ - $el.data('zf-plugin', new plugin($(this), opts)); + $el.data('zfPlugin', new plugin($(this), opts)); }catch(er){ console.error(er); }finally{ -- 2.47.2