Skip to content

Commit 3cea9f5

Browse files
committed
Merge pull request airbnb#73 from airbnb/event-payloads
[events] add events section, pass a hash not a raw value
2 parents 8335ffa + 412a7f9 commit 3cea9f5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
1. [Naming Conventions](#naming-conventions)
2424
1. [Accessors](#accessors)
2525
1. [Constructors](#constructors)
26+
1. [Events](#events)
2627
1. [Modules](#modules)
2728
1. [jQuery](#jquery)
2829
1. [ES5 Compatibility](#es5)
@@ -1188,6 +1189,37 @@
11881189
**[[⬆]](#TOC)**
11891190
11901191
1192+
## <a name='events'>Events</a>
1193+
1194+
- 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:
1195+
1196+
```js
1197+
// bad
1198+
$(this).trigger('listingUpdated', listing.id);
1199+
1200+
...
1201+
1202+
$(this).on('listingUpdated', function(e, listingId) {
1203+
// do something with listingId
1204+
});
1205+
```
1206+
1207+
prefer:
1208+
1209+
```js
1210+
// good
1211+
$(this).trigger('listingUpdated', { listingId : listing.id });
1212+
1213+
...
1214+
1215+
$(this).on('listingUpdated', function(e, data) {
1216+
// do something with data.listingId
1217+
});
1218+
```
1219+
1220+
**[[⬆]](#TOC)**
1221+
1222+
11911223
## <a name='modules'>Modules</a>
11921224
11931225
- 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.

0 commit comments

Comments
 (0)