Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use jQuery UI's Widget factory, which gives you that functionality out-of-the-box:</p> <pre><code>$.widget("vtortola.formcontroller", { _create: function() { // Use `this.element` to initialize the element. // You can also use this.options to get the options object that was passed. }, _destroy: function() { // You can unbind events here to remove references from the DOM, so is'll get // deleted by the garbage collector. }, submit: function() { /* ... */ } }); // Example $('#form').formcontroller({foo: 123}); // Calls `_create()` with the supplied options $('#form').formcontroller('submit'); // Calls the `submit` method </code></pre> <p>The widget factory also gives you options setter/getters, default options, and other cool stuff. See <a href="http://wiki.jqueryui.com/w/page/12138135/Widget%20factory" rel="nofollow">the official docs</a> and the <a href="http://bililite.com/blog/understanding-jquery-ui-widgets-a-tutorial/" rel="nofollow">tutorial at bililite</a> for more details. If you aren't using jQuery UI for other stuff, you can use the <a href="https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.widget.js" rel="nofollow">widget factory code</a> as a stand-alone, its not depended on other parts of jQuery UI.</p> <p>If you prefer to it without using jQuery UI's widget factory, you can store the objects directly on <code>.data()</code> (which is also what the widget factory does), no need to keep a collection of them and keep track of that manually. Your code would be something along the lines of:</p> <pre><code>$.fn.formController = function () { var args = arguments; return this.each(function (i, item) { var $this = $(this), _obj; // When calling `$(..)`.formController() with arguments when the object already // exists, it treats the first argument as the method name and passes the rest // as arguments to that method. if (arguments.length &amp;&amp; (_obj = $this.data('FormController'))) { _obj[args[0]].apply(_obj, Array.prototype.slice.call(args, 1)); } else { $this.data('FormController', new FormController(item)); } }); }; </code></pre>
    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.
    3. 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