Events

  • 25.1 When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass an object literal (also known as a “hash”) instead of a raw value. This allows a subsequent contributor to add more data to the event payload without finding and updating every handler for the event. For example, instead of:

    1. // bad
    2. $(this).trigger('listingUpdated', listing.id);
    3. // ...
    4. $(this).on('listingUpdated', (e, listingID) => {
    5. // do something with listingID
    6. });

    prefer:

    1. // good
    2. $(this).trigger('listingUpdated', { listingID: listing.id });
    3. // ...
    4. $(this).on('listingUpdated', (e, data) => {
    5. // do something with data.listingID
    6. });