Testcase for jQuery Ticket 6398

submit and custom event on non DOM elements

by jitter

HTML

<h1>Binding The Submit Event On Objects In jQuery</h1>
<p>This is a paragraph.</p>

JavaScript

/* Enable binding custom submit event to non DOM elements in IE */
if (!jQuery.support.submitBubbles) {
    var oldSpecialSubmitSetup = jQuery.event.special.submit.setup;
    jQuery.event.special.submit.setup = function(data, namespaces) {
        if (this.nodeName) {
            return oldSpecialSubmitSetup(data, namespaces);
        } else {
            return false;
        }
    };
}


// I am a tiny class  to control a paragraph.

function ParaController(target) {
    var self = this;
    // Keep the  target reference as the UI aspect of this
    // controller instance.
    this.ui = target;
    // Bind the  double-click event to the UI aspect.
    this.ui.dblclick(function(event) {
        // Use  this internal, UI-click as a reason to
        // trigger a custom event on this component.
        jQuery(self).trigger({
            type: "submit",
            text: jQuery.trim(self.ui.text())
        });
    });
}

//  -------------------------------------------------- //
//  -------------------------------------------------- //
//  When the DOM is ready initialize the scripts.
jQuery(function() {
    //  Create an instance of the para controller.
    var paraController = new ParaController($("p"));
    // Bind to the custom click event on the component.
    $(paraController).bind("submit", function(event) {
        // Alert the text submitted with this event.
        alert(event.text);
    });
});