Detect currently selected source of custom elements
You can see how this is working by opening craft/app/resources/js/craft.js and searching for the Craft.EntryIndex class definition.

Craft.EntryIndex is an extension of Craft.BaseElementIndex, and is used to govern all Entry indexes in Craft (including Entry selection modals). We pull that off with this line of code, which you’ll find immediately after the class definition:



Craft.registerElementIndexClass('Entry', Craft.EntryIndex);


The logic governing the “New Entry” button is a little complicated, and it relies on an initial version of the button HTML already being available on the page (thanks to the craft/app/templates/entries/_edit.html template), but all the magic happens within its onSelectSource() method, including updating the URL.