<p>To understand how it works under the hood, read the <ahref="https://jcbrand.github.io/pluggable.js/docs/pluggable.html">annotated source code</a>.</p>
<h2>
<aid="live-demo-of-the-example-code" class="anchor" href="#live-demo-of-the-example-code" aria-hidden="true"><spanaria-hidden="true" class="octicon octicon-link"></span></a>Live demo of the example code</h2>
<pid="notifications"></p>
<p>
Here's a small example which shows how pluggable.js can be used to
make a private, closured Javascript object pluggable.
</p>
<p>
Further below we'll show the example code and explain how it works.
</p>
<fieldset>
<inputtype="button" class="btn btn-example" id="notify" value="Click me to show a notification"/>
<inputtype="button" class="btn btn-example" id="enable" value="Click me to enable the plugin"/>
<aid="example-code-of-a-module-to-be-made-pluggable" class="anchor" href="#example-code-of-a-module-to-be-made-pluggable" aria-hidden="true"><spanaria-hidden="true" class="octicon octicon-link"></span></a>Example code of a module to be made pluggable</h3>
<p>Let's now look at the code for our module (which will go into a file called <code>app.js</code>):</p>
<divclass="highlight highlight-source-js"><pre><spanclass="pl-c">// An example application, used to demonstrate how pluggable.js can</span>
<spanclass="pl-c">// allow a module to be made pluggable.</span>
<spanclass="pl-c">/* Displays a notification message */</span>
<spanclass="pl-k">var</span> el <spanclass="pl-k">=</span><spanclass="pl-c1">document</span>.<spanclass="pl-c1">createElement</span>(<spanclass="pl-s"><spanclass="pl-pds">'</span>p<spanclass="pl-pds">'</span></span>);
el <spanclass="pl-k">=</span><spanclass="pl-c1">document</span>.<spanclass="pl-c1">getElementById</span>(<spanclass="pl-s"><spanclass="pl-pds">'</span>notifications<spanclass="pl-pds">'</span></span>).<spanclass="pl-c1">appendChild</span>(el);
<spanclass="pl-c1">module</span>.<spanclass="pl-en">_showNotification</span>(<spanclass="pl-s"><spanclass="pl-pds">'</span>This is a notification.<spanclass="pl-pds">'</span></span>);
<spanclass="pl-c">// When overriding a method, you can still call the original method</span>
<spanclass="pl-c">// via `this.__super__`. To properly call with the proper `this`</span>
<spanclass="pl-c">// context and parameters, use `.apply(this, arguments)`.</span>
<spanclass="pl-k">var</span> el <spanclass="pl-k">=</span><spanclass="pl-v">this</span>.<spanclass="pl-smi">__super__</span>.<spanclass="pl-smi">_showNotification</span>.<spanclass="pl-c1">apply</span>(<spanclass="pl-v">this</span>, <spanclass="pl-v">arguments</span>);
<spanclass="pl-c">// Now we simply set another class on the element containing</span>
<spanclass="pl-c">// the notifications, so that they'll appear in a different</span>
<p>The plugin has three important parts to it. Firstly, a call to register the
plugin must be made, and then passed in with this call is an object which
represents the plugin itself.</p>
<p>This plugin object has an <code>overrides</code> key, which is another object containing methods
and objects that will override similarly named methods and object properties on the
module itself.</p>
<p>Additionally, the <code>initializePlugins</code> method has been called on the
<code>pluginSocket</code> instance.</p>
<p>This happens inside our module in app.js and this methods should generally be
called once, after all plugins have been registered and after the module being
made pluggable has itself been initialized.</p>
<p>In the <code>initialize</code> method, you have access the module's scope, which would
otherwise not be available to you.</p>
<p>So, as you can see, the plugin lets us achieve our three goals stated earlier:</p>
<h3>
<aid="access-the-closured-scope-of-this-module" class="anchor" href="#access-the-closured-scope-of-this-module" aria-hidden="true"><spanaria-hidden="true" class="octicon octicon-link"></span></a>1. Access the closured scope of this module.</h3>
<p>We have access to the module and all its properties and methods inside the
<code>initialize</code> method of our plugin.</p>
<h3>
<aid="add-new-methods-to-the-module" class="anchor" href="#add-new-methods-to-the-module" aria-hidden="true"><spanaria-hidden="true" class="octicon octicon-link"></span></a>2. Add new methods to the module.</h3>
<p>We can add new methods to our module, either inside the <code>initialize</code> method of
our plugin, or by stating them declaratively in <code>overrides</code> object.</p>
<h3>
<aid="override-or-extend-existng-methods-private-or-public-on-this-module" class="anchor" href="#override-or-extend-existng-methods-private-or-public-on-this-module" aria-hidden="true"><spanaria-hidden="true" class="octicon octicon-link"></span></a>3. Override or extend existng methods (private or public) on this module.</h3>
<p>We can override existing methods and object properties via the <code>overrides</code>
object.</p>
</section>
<h2>Contact</h2>
<p>For issues and requests, please use the project's <ahref="https://github.com/jcbrand/pluggable.js/issues">issue tracker</a>.</p>
<p>You can reach me via my website's <ahref="http://opkode.com/contact.html">contact form</a> and you can also <ahref="https://twitter.com/jcopkode">follow me on twitter</a>.</p>