Plone 5: Decorating a textarea in addform pop

In Plone 5: creating a new content objects from the folder_contents view performs a POST to ++add++SOMETYPE in order to retrieve the addform and display it as popup.

I have a custom Dexterity and a custom Dexterity field which renders as a textarea and attaches the ACE editor
to the textarea.

In Plone 4 is was trivial doing this using $(document).ready(..)..

Things in Plone 5 are a bit more complicated. So what is the canonical way for decorating the textarea after it was rendered inside the add form popup. Should I hook using jQuery into some DOM change event listener?

-aj

Ah yes. That is a bit more dicey.

You'll need to utilize some of the events that patterns fire off. It'll be something like this:

$('.pat-structure').off('init.structure.patterns').on('init.structure.patterns', function(){
  // so now we know that  structure is loaded and we can bind the add actions
  $('.btn-group.addnew').off('init.modal.patterns').on('init.modal.patterns', function(){
    // your code here
  });
});

In one project I used this snippet to make inline scripts execute after modals are loaded:

// execute inline scripts after ajax load of modal
$(document).ajaxSuccess(function(e, xhr, settings, data) {
    var scripts = [];
    $.buildFragment([data], document, scripts);
    if (scripts.length) {
        $.each(scripts, function() {
            $.globalEval( this.text || this.textContent || this.innerHTML || "" );
        });
    }
});

Your mileage may vary depending on what exactly those scripts do, of course.