Skip to content

Commit 6ee2d53

Browse files
committed
Use jQuery#on for delegation of events
jQuery#on lets you capture events on a parent element for a specific selector, instead of binding a different event for each element. This is better for two reasons: 1) on large applications, you conserve memory (and prevent leaks) by not having a ton of events 2) when you add elements to a list dynamically, you don't have to refresh the event listeners
1 parent 5e7f1ff commit 6ee2d53

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
$(document).ready ->
2-
$('.close').click ->
2+
$('.close').on "click", ->
33
$('#showUser').fadeOut()
44

55
$('form#new_user').on "ajax:success", (data, status, xhr) ->
6-
$('#listContainer').load('userlist/', -> bindListBehavior())
6+
$('#listContainer').load('userlist/')
77

8-
bindListBehavior = ->
9-
$('span.showUser').click ->
10-
userid = $(this).attr('userid')
11-
$('#userDiv').load('users/' + userid, -> $('#showUser').show() )
8+
$('#listContainer').on 'click', 'span.showUser', ->
9+
userid = $(this).attr('userid')
10+
$('#userDiv').load('users/' + userid, -> $('#showUser').show() )
1211

13-
$('.delete_user').bind 'ajax:success', ->
14-
$(this).closest('tr').fadeOut()
15-
16-
# only major change in coffeescript translation: bindListBehavior must be called after it is defined
17-
bindListBehavior()
12+
$('#listContainer').on 'ajax:success', '.delete_user', ->
13+
$(this).closest('tr').fadeOut()

0 commit comments

Comments
 (0)