diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 4e830b530b14..c85528ed9f7d 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -6,6 +6,7 @@ */ import { + basicJQueryInterface, defineJQueryPlugin, getSelectorFromElement, getUID, @@ -277,19 +278,7 @@ class ScrollSpy extends BaseComponent { // Static static jQueryInterface(config) { - return this.each(function () { - const data = ScrollSpy.getInstance(this) || new ScrollSpy(this, typeof config === 'object' ? config : {}) - - if (typeof config !== 'string') { - return - } - - if (typeof data[config] === 'undefined') { - throw new TypeError(`No method named "${config}"`) - } - - data[config]() - }) + return basicJQueryInterface(this, ScrollSpy, config) } } diff --git a/js/src/util/index.js b/js/src/util/index.js index cc35d8a37c4c..8802a0b8e04a 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -1,3 +1,5 @@ +import Data from '../dom/data' + /** * -------------------------------------------------------------------------- * Bootstrap (v5.0.0-beta3): util/index.js @@ -230,6 +232,22 @@ const defineJQueryPlugin = (name, plugin) => { }) } +const basicJQueryInterface = (jq, Plugin, config) => { + return jq.each(function () { + const data = Data.get(this, Plugin.DATA_KEY) || new Plugin(this, typeof config === 'object' ? config : {}) + + if (typeof config !== 'string') { + return + } + + if (typeof data[config] === 'undefined') { + throw new TypeError(`No method named "${config}"`) + } + + data[config]() + }) +} + export { getUID, getSelectorFromElement, @@ -247,5 +265,6 @@ export { getjQuery, onDOMContentLoaded, isRTL, + basicJQueryInterface, defineJQueryPlugin }