Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to define your default name and foo events in your defaults declaration:</p> <pre><code>$.fn.myPlugin.defaults = { name: 'test', onFoo: function() { alert(this.name); }, onFoo2: function() { // your default behaviour for foo2 } }; </code></pre> <p>Then, when someone calls your plugin, they can override the defaults, in this case <code>name</code>:</p> <pre><code> $("#myControl").myPlugin({ name: 'test2' }); </code></pre> <p>Note that they don't need to override onFoo, because it will display an alert with test2. Anyway, if they need to override it to do something different, then they should:</p> <pre><code> $("#myControl").myPlugin({ name: 'test2', onFoo: function() { alert('onFoo overrired'); }, onFoo2: function() { alert('onFoo2 overrired'); } }); </code></pre> <p>In your plugin, you invoke the foo methods as</p> <pre><code>(function($) { $.fn.myPlugin = function(options) { var opt = $.extend({}, $.fn.myPlugin.defaults, options); // if onFoo is defined then call it if (opt.onFoo) { opt.onFoo(); } // if onFoo2 is defined then call it if (opt.onFoo2) { opt.onFoo2(); } } $.fn.myPlugin.defaults = { name: 'test', onFoo: function() { alert(this.name); }, onFoo2: function() { // your default behaviour for foo2 } }; }); </code></pre> <p>You should use this technique for public methods/properties that you want to expose to the users of your plugin. I didn't tested but should work</p> <p><strong>Edit</strong> You need to check if the event is set before calling it:</p> <pre><code>// if onFoo is defined (not null) then call it if (opt.onFoo) { opt.onFoo(); } </code></pre> <p>You are setting already an event for onFoo and onFoo2, but the user of your plugin might choose to disable it:</p> <pre><code> $("#myControl").myPlugin({ onFoo: null }); </code></pre> <p>In this case, although you have defined an <code>onFoo</code> event, the user of your plugin decided to ignore it, by setting it to <code>null</code>. So, even though you have defined an event, you never know what others will do with it, therefore it's better to be on the safe side and check for nullity.</p> <p>Once again, you need to be careful with what you expose to the end user, because setting/unsetting events should not break the basic functionality of your plugin</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload