Skip to content

Commit 93e7d6f

Browse files
committed
Include parent/container in before/after add/remove has_many events
1 parent 4a4401b commit 93e7d6f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

app/assets/javascripts/active_admin/lib/has_many.js.coffee

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,41 @@ $ ->
22
# Provides a before-removal hook:
33
# $ ->
44
# # This is a good place to tear down JS plugins to prevent memory leaks.
5-
# $(document).on 'has_many_remove:before', '.has_many_container', (e, fieldset)->
5+
# $(document).on 'has_many_remove:before', '.has_many_container', (e, fieldset, container)->
66
# fieldset.find('.select2').select2 'destroy'
77
#
8+
# # If you need to do anything after removing the items you can use the
9+
# has_many_remove:after hook
10+
# $(document).on 'has_many_remove:after', '.has_many_container', (e, fieldset, container)->
11+
# list_item_count = container.find('.has_many_fields').length
12+
# alert("There are now #{list_item_count} items in the list")
13+
#
814
$(document).on 'click', 'a.button.has_many_remove', (e)->
915
e.preventDefault()
1016
parent = $(@).closest '.has_many_container'
1117
to_remove = $(@).closest 'fieldset'
1218
recompute_positions parent
1319

14-
parent.trigger 'has_many_remove:before', [to_remove]
20+
parent.trigger 'has_many_remove:before', [to_remove, parent]
1521
to_remove.remove()
16-
parent.trigger 'has_many_remove:after', [ to_remove ]
22+
parent.trigger 'has_many_remove:after', [to_remove, parent]
1723

1824
# Provides before and after creation hooks:
1925
# $ ->
2026
# # The before hook allows you to prevent the creation of new records.
21-
# $(document).on 'has_many_add:before', '.has_many_container', (e)->
27+
# $(document).on 'has_many_add:before', '.has_many_container', (e, container)->
2228
# if $(@).children('fieldset').length >= 3
2329
# alert "you've reached the maximum number of items"
2430
# e.preventDefault()
2531
#
2632
# # The after hook is a good place to initialize JS plugins and the like.
27-
# $(document).on 'has_many_add:after', '.has_many_container', (e, fieldset)->
33+
# $(document).on 'has_many_add:after', '.has_many_container', (e, fieldset, container)->
2834
# fieldset.find('select').chosen()
2935
#
3036
$(document).on 'click', 'a.button.has_many_add', (e)->
3137
e.preventDefault()
3238
parent = $(@).closest '.has_many_container'
33-
parent.trigger before_add = $.Event 'has_many_add:before'
39+
parent.trigger before_add = $.Event('has_many_add:before'), [parent]
3440

3541
unless before_add.isDefaultPrevented()
3642
index = parent.data('has_many_index') || parent.children('fieldset').length - 1
@@ -41,7 +47,7 @@ $ ->
4147

4248
fieldset = $(html).insertBefore(@)
4349
recompute_positions parent
44-
parent.trigger 'has_many_add:after', [fieldset]
50+
parent.trigger 'has_many_add:after', [fieldset, parent]
4551

4652
$(document).on 'change','.has_many_container[data-sortable] :input[name$="[_destroy]"]', ->
4753
recompute_positions $(@).closest '.has_many'

0 commit comments

Comments
 (0)