0

Is there a global object that Bootstrap creates when initializing one of its components, e.g. Tabs? How does it work?

Using Bootstrap 3.2.

1
  • 1
    $.fn.tabs = ... Is that the answer to your question? Commented Aug 23, 2014 at 20:01

1 Answer 1

2

First they define

var Tab = function (element) { this.element = $(element) } 

Then they go further and prototype Tab:

Tab.prototype.activate = function (element, container, callback) { var $active = container.find('> .active') var transition = callback //... } 

And finally they add Tab as a jQuery method using their Plugin factory:

function Plugin(option) { return this.each(function () { var $this = $(this) var data = $this.data('bs.tab') if (!data) $this.data('bs.tab', (data = new Tab(this))) if (typeof option == 'string') data[option]() }) } $.fn.tab = Plugin $.fn.tab.Constructor = Tab 

Plugin seems to be something like the jQuery UI Plugin Factory, so you call $("#a").tab("method") and the Plugin object will call the corresponding method on a Tab object.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh... when I was on their page I was able to type "Plugin" into the console and see that it was a function, but the only thing that appeared in Object.keys(Plugin) was a toSTring function. I should have seen the $.fn.tab and Plugin.call($(this), 'show'); !
@user1809836 No, you shouldn't. It's the factory pattern. Plugin is used to insatiate the object that really handles the calls to the jQuery plugin, it's a bit like a bridge. It's similar to this: learn.jquery.com/jquery-ui/widget-factory

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.