|
2 | 2 | # Provides a before-removal hook: |
3 | 3 | # $ -> |
4 | 4 | # # 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)-> |
6 | 6 | # fieldset.find('.select2').select2 'destroy' |
7 | 7 | # |
| 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 | + # |
8 | 14 | $(document).on 'click', 'a.button.has_many_remove', (e)-> |
9 | 15 | e.preventDefault() |
10 | 16 | parent = $(@).closest '.has_many_container' |
11 | 17 | to_remove = $(@).closest 'fieldset' |
12 | 18 | recompute_positions parent |
13 | 19 |
|
14 | | - parent.trigger 'has_many_remove:before', [to_remove] |
| 20 | + parent.trigger 'has_many_remove:before', [to_remove, parent] |
15 | 21 | to_remove.remove() |
16 | | - parent.trigger 'has_many_remove:after', [ to_remove ] |
| 22 | + parent.trigger 'has_many_remove:after', [to_remove, parent] |
17 | 23 |
|
18 | 24 | # Provides before and after creation hooks: |
19 | 25 | # $ -> |
20 | 26 | # # 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)-> |
22 | 28 | # if $(@).children('fieldset').length >= 3 |
23 | 29 | # alert "you've reached the maximum number of items" |
24 | 30 | # e.preventDefault() |
25 | 31 | # |
26 | 32 | # # 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)-> |
28 | 34 | # fieldset.find('select').chosen() |
29 | 35 | # |
30 | 36 | $(document).on 'click', 'a.button.has_many_add', (e)-> |
31 | 37 | e.preventDefault() |
32 | 38 | 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] |
34 | 40 |
|
35 | 41 | unless before_add.isDefaultPrevented() |
36 | 42 | index = parent.data('has_many_index') || parent.children('fieldset').length - 1 |
|
41 | 47 |
|
42 | 48 | fieldset = $(html).insertBefore(@) |
43 | 49 | recompute_positions parent |
44 | | - parent.trigger 'has_many_add:after', [fieldset] |
| 50 | + parent.trigger 'has_many_add:after', [fieldset, parent] |
45 | 51 |
|
46 | 52 | $(document).on 'change','.has_many_container[data-sortable] :input[name$="[_destroy]"]', -> |
47 | 53 | recompute_positions $(@).closest '.has_many' |
|
0 commit comments