Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery extension and closures
    primarykey
    data
    text
    <p>I'm learning javascript, and in the way I'm trying to learn more about jQuery. I have created a very simple "form controller" in JS, so when I create the object passing the form as parameter, it gets the events wired and hijacks the submit:</p> <pre><code>var FormController = function (form) { // private field var _form = $(form); var _url = _form.attr('action'); var _isValid = false; $(form).submit(function (e) { submitForm(); e.preventDefault(); }); var disableAll = function () { _form.find('select,input,textarea').attr('disabled', 'disabled') }; var enableAll = function () { _form.find('select,input,textarea').removeAttr('disabled') }; var submitForm = function () { disableAll(); _isValid = true; if (_isValid) { $.ajax({ url: _url, type: 'POST', success: function (data, textStatus, jqXHR) { enableAll(); var obj = jQuery.parseJSON(jqXHR.responseText); print(data.Result ? data.Result : "[No Result]"); print(textStatus.toString()); }, error: function (data, textStatus, jqXHR) { print(textStatus); _form.css('border', '1px solid Red'); enableAll(); } }); } }; // public fields and functions return { getIsValid: function () { return _isValid; }, submit: function () { return submitForm(); } }; }; </code></pre> <p>It works as expected. Now I would like to create a jQuery extension, so I can apply that object to the results:</p> <pre><code>$.fn.formController = function () { return this.each(function (i, item) { new FormController(item); }); }; </code></pre> <p>I also works as expected, but... will the GC collect that object? Or because the events are wired, it counts as referenced?</p> <p>Now, I would like to keep the instances of my controllers available, so I can manipulate them after creation, for example for invoking methods. Something like this:</p> <pre><code>$('#myForm').formController('submit'); </code></pre> <p>I have tried several ways to do it, but I cannot get it. I have tried to put a closure with a object that keep track of the items, etc... but I just got mess with "this". </p> <p>What would be the correct way to do this? And I assume that everything I have done so far could be wrong even if it works.</p> <p>Thanks.</p> <p><strong>UPDATE</strong></p> <p>Sorry if the question was not clear enough. What I am aiming for is a way I can do : <code>$('#myForm').formController('submit');</code> , and then the funcion will find the "FormController" object associated with that HTML object, and invoke this "submit" member.</p> <p>I want to achieve something like this:</p> <pre><code>$.fn.formController = function (name) { if(!document._items) document._items = []; if (name) { return this.each(function (i, item) { var id = $(item).data('form-controller'); if (id) { var fc = document._items[id]; var member = fc[name]; if (member) { if (typeof member == 'function') member(); } } }); } else { return this.each(function (i, item) { var id = document._items.push(new FormController(item)) - 1; $(item).data('form-controller', id.toString()); }); } }; </code></pre> <p>The problem in this approach is that I'm keeping a collection as global object, and what I want is make it internal. I have tried with a closure, but I only got into problems with "this" (that points to DOMwindow), or the "_items" var being empty. </p> <p>What would be the correct way to approach this?</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. 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