Skip to content

Instantly share code, notes, and snippets.

@ethan-deng
Last active November 4, 2015 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethan-deng/83eddcb195085b8e1902 to your computer and use it in GitHub Desktop.
Save ethan-deng/83eddcb195085b8e1902 to your computer and use it in GitHub Desktop.

Add Event Handler to Aysnc Loaded UI Component without Wait

Usually when you want to add a event handler to a dynamically loaded UI compoent is to wait for the UI is generated and then attached the event handler. If this is the only way to do it, and if you are writing a shared library, you have to provide a "update"/"refresh" function to run to attach the event handlers in the "wait" function.

$.when($.ajax(url)).done(function(result){
  $('<div>').appendTo(myDoc).click(function(){
    ......
  });
});

There is another way to attach event handler without wait for async call.

  $(document)
    .on('click.bs.dropdown.data-api', clearMenus)
    .on('click.bs.dropdown.data-api', '.dropdown form', function (e) { e.stopPropagation() })
    .on('click.bs.dropdown.data-api', toggle, Dropdown.prototype.toggle)
    .on('keydown.bs.dropdown.data-api', toggle, Dropdown.prototype.keydown)
    .on('keydown.bs.dropdown.data-api', '[role="menu"]', Dropdown.prototype.keydown)
    .on('keydown.bs.dropdown.data-api', '[role="listbox"]', Dropdown.prototype.keydown)

Here is the jQuery document on on:

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector. ...

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment