3

When a user switches Entry Types in the control panel, Craft.EntryTypeSwitcher is trigged which in turn triggers onTypeChange.

Is there callback that a plugin can access to run some additional JavaScript after this event has occurred?

===== Edit to show Current work-around as per Justin's answer below ======

(function($) {
    Craft.LoadOverride = Garnish.Base.extend({
        init: function()
        {
            this.addListener(Garnish.$win, 'resize', 'runOverride');
        },
        runOverride: function()
        {
            console.log('Triggered!');
        }
    });
})(jQuery);
3

It looks like the only easy way to hook into that event is to listen for the resize event on the window object. On line 55, it is triggering resize(). Looking at the code, I am not seeing any other way to listen for these events. The Craft.EntryTypeSwitcher class is getting instantiated but not getting assigned to any variable in the DOM, so there is no way for you to grab that instance (from what I can tell anyway).

  • Hi Justin, thanks for the answer. That was kind of what I was seeing as well. Listening for the resize might be ok temporarily, but I'm really only looking for this code to be executed at the end of this specific event. Feature Request! :-) – Peter Tell Jan 9 '15 at 20:37
  • Just a note on this: I was able to get this working by listening for the resize event as Justin has described. I'm going to wait a short time to see if P&T weigh in on this as it would be nice to access the instance directly. Otherwise, I'll mark it as correct. – Peter Tell Jan 9 '15 at 21:03
  • Exactly my thoughts on the topic. Glad you got that working for now at least. – Justin Kimbrell Jan 9 '15 at 23:21
3

The most reliable way I've found to listen for the entry type switching would be to add an ajaxComplete listener to the document object, testing the call's URL for the switchEntryType action handle:

$(document).ajaxComplete(myAjaxCompleteHandler);

function myAjaxCompleteHandler( e, status, requestData )
{
    if ( requestData.url.indexOf( 'switchEntryType' ) > -1 ) {
        // Entry type switched. Do your stuff!
        ...
    }
}

If you need the new/active entry type's ID after the switch, the easiest way to get that would be by getting the entry type dropdown's value:

var entryTypeId = $( '#entryType' ).val();

Other data, like all the fields and the section ID, is available as part of the requestData object, specifically as a serialized string under requestData.data. Here's a very basic deserialization to get to the entry type and section IDs:

var data = {},
    params = $.map( unescape( requestData.data ).split( '&' ), function ( param ) {
        param = param.split( '=' );
        data[ param[ 0 ] ] = param[ 1 ] || null;
    } ),
    sectionId = parseInt( data.sectionId ),
    entryTypeId = parseInt( data.typeId );

}

The field data is more complex – many fields will be arrays which aren't handled well by the above example – but there's several jQuery plugins to help out with that stuff if you need it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.