mirror of
https://github.com/airbnb/javascript.git
synced 2026-04-25 03:00:19 -04:00
Merge pull request #73 from airbnb/event-payloads
[events] add events section, pass a hash not a raw value
This commit is contained in:
32
README.md
32
README.md
@@ -23,6 +23,7 @@
|
||||
1. [Naming Conventions](#naming-conventions)
|
||||
1. [Accessors](#accessors)
|
||||
1. [Constructors](#constructors)
|
||||
1. [Events](#events)
|
||||
1. [Modules](#modules)
|
||||
1. [jQuery](#jquery)
|
||||
1. [ES5 Compatibility](#es5)
|
||||
@@ -1188,6 +1189,37 @@
|
||||
**[[⬆]](#TOC)**
|
||||
|
||||
|
||||
## <a name='events'>Events</a>
|
||||
|
||||
- When attaching data payloads to events (whether DOM events or something more proprietary like Backbone events), pass 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:
|
||||
|
||||
```js
|
||||
// bad
|
||||
$(this).trigger('listingUpdated', listing.id);
|
||||
|
||||
...
|
||||
|
||||
$(this).on('listingUpdated', function(e, listingId) {
|
||||
// do something with listingId
|
||||
});
|
||||
```
|
||||
|
||||
prefer:
|
||||
|
||||
```js
|
||||
// good
|
||||
$(this).trigger('listingUpdated', { listingId : listing.id });
|
||||
|
||||
...
|
||||
|
||||
$(this).on('listingUpdated', function(e, data) {
|
||||
// do something with data.listingId
|
||||
});
|
||||
```
|
||||
|
||||
**[[⬆]](#TOC)**
|
||||
|
||||
|
||||
## <a name='modules'>Modules</a>
|
||||
|
||||
- The module should start with a `!`. This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated.
|
||||
|
||||
Reference in New Issue
Block a user