How to bind to 'sonata-collection-item-added' in sonata_type_collection fields?

717 views
Skip to first unread message

Ludescher Antal

unread,
Mar 23, 2013, 3:19:18 PM3/23/13
to sonata...@googlegroups.com

In sonata_type_collection fields the "Add new" button has no "sonata-collection-add" class therefore the following code is never executed:

jQuery(subject).on('click', '.sonata-collection-add', function(event) { ... }

sonata-collection-item-added is never triggered.
The documentation says:
A jQuery event is fired after a row has been added(sonata-collection-item-added)

How to bind to 'sonata-collection-item-added' in sonata_type_collection fields?

Huy Vũ

unread,
Mar 26, 2013, 6:44:48 AM3/26/13
to sonata...@googlegroups.com
I would love to know too.

Huy Vũ

unread,
Mar 28, 2013, 4:53:36 AM3/28/13
to sonata...@googlegroups.com
I has just found out an alternative method, using livequery. It works for me. Here is my code to display image

<script type="text/javascript">   
        $(function(){
            
            $("#field_container_{{ admin.uniqId }}_images").livequery(function() {
                image_preview();
            });
        });
</script>

Ludescher Antal

unread,
Mar 29, 2013, 1:05:54 AM3/29/13
to sonata...@googlegroups.com
Nice idea!
Wouldn't this also fire on every other event (when removing lines) ? I think an event check should be added to be on the safe side:
$("#field_container_{{ admin.uniqId }}_images").livequery(function(event) {
                if('event' == '_some_event_name_')
                image_preview();
            });

Another solution could be to add the sonata-collection-add class name to the button, but I don't know how to do that elegantly.

Huy Vũ

unread,
Mar 29, 2013, 9:32:58 AM3/29/13
to sonata...@googlegroups.com
Why you don't use jquery addClass? Something like:

$(".sonata-ba-action").addClass("sonata-collection-add");

If you have more than one button, check their parents to add to the right one:

$("#field_actions_{{admin.uniqId}}_images > a.sonata-ba-action").addClass("sonata-collection-add");
Reply all
Reply to author
Forward
0 new messages