<p>It looks like the only easy way to hook into that event is to listen for the <code>resize</code> event on the <code>window</code> object. On line 55, it is triggering <code>resize()</code>. Looking at the code, I am not seeing any other way to listen for these events. The <code>Craft.EntryTypeSwitcher</code> 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).</p>

CC BY-SA 3.0
2015-01-09T20:37:15.717Z
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
CC BY-SA 3.0
2015-01-09T21:03:15.573Z
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
CC BY-SA 3.0
2015-01-09T23:21:19.813Z
Exactly my thoughts on the topic. Glad you got that working for now at least.
Justin Kimbrell
CC BY-SA 3.0
2015-01-09T20:10:00.190Z
2015-01-09T20:10:00.190Z
Justin Kimbrell
<p>The most reliable way I've found to listen for the entry type switching would be to add an <code>ajaxComplete</code> listener to the <code>document</code> object, testing the call's URL for the <code>switchEntryType</code> action handle:</p>

<pre><code>$(document).ajaxComplete(myAjaxCompleteHandler);

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

<p>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:</p>

<pre><code>var entryTypeId = $( '#entryType' ).val();
</code></pre>

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

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

}
</code></pre>

<p>The field data is more complex – many fields will be <em>arrays</em> 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 :)</p>

CC BY-SA 3.0
2015-04-27T01:16:54.723Z
2015-05-24T14:33:33.213Z
2015-05-24T14:33:33.213Z
Mats Mikkel Rummelhoff
<p>When a user switches Entry Types in the control panel, <code>Craft.EntryTypeSwitcher</code> is trigged which in turn triggers <code>onTypeChange</code>.</p>

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

<p>===== Edit to show Current work-around as per Justin's answer below ======</p>

<pre class="lang-javscript prettyprint-override"><code>(function($) {
    Craft.LoadOverride = Garnish.Base.extend({
        init: function()
        {
            this.addListener(Garnish.$win, 'resize', 'runOverride');
        },
        runOverride: function()
        {
            console.log('Triggered!');
        }
    });
})(jQuery);
</code></pre>

<plugins><javascript><garnish>
Is there an accessible callback when Craft.EntryTypeSwitcher is run?
# Is there an accessible callback when Craft.EntryTypeSwitcher is run?

(Asked by: username_0 on 2015-01-09)

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);

**Tags:** plugins, javascript, garnish

---

## 2 answers

### Answer by username_1 on 2015-01-09. Score: 3 | Accepted answer

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).

> **Comments:**
> **username_0**: 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! :-)  
> **username_0**: 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.  
> **username_1**: Exactly my thoughts on the topic. Glad you got that working for now at least.  

---

### Answer by username_2 on 2015-04-27. Score: 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 :)


---

