Building Enterprise HTML5 Application

jQuery.fn.bEntity

Convenenient jQuery brite extension return the entity reference object of the closest matching HTMLElement with the matching data-entity attribute

$.fn.bEntity(entityType)

  • entityType {string} : If present, will look for the closest element matching this entityType, otherwise will match the first HTMLElement with a data-entity attribute.
  • returns {Object} the entity reference object with the following properties.

    • .type {string}: The data-entity attribute value.
    • .id {string}: The data-entity-id attribute value.
    • .name {string}: The data-entity-name attribute value if present. This is not always used, but can be useful to allow the user interface logic to have this common property in the DOM rather than doing a DAO request to get it.

    Examples

    For example, for the following HTML.

    <div data-entity="Project" data-entity-id="001">  
      <ul>
        <li data-entity="Task" data-entity-id="101">My Task 1
           <button class="delete">Delete</button>
        </li>
      </ul>  
    </div>
    
    var taskRef = $("button.delete").bEntity();
    // taskRef.id   === "101"
    // taskRef.type === "Task"
    // taskRef.name === undefined (because, was not present as attribute)
    
    // this will return the same object values as above, 
    // since bEntity use the jQuery.fn.closest to get its matching parent element
    var sameTaskRef = $("li").bEntity();
    
    var projectRef = $("button.delete").bEntity("Project");
    // projectRef.id === "001"
    // projectRef.type === "Project"
    // projectRef.name === undefined (because, was not present as attribute)
    
    // Note that bEntity() returns the first entity reference for the first match 
    // HTMLElement.
    

Ask, learn, share about brite.js

Go to brite.js G+ community