Although the question may appear vague initially, I struggled to find a better way to convey my idea. Let me elaborate on it in detail.
In my SPA application, I have MasterController connected to the <html>
tag. This MasterController consists of all the logic and models required to control the following UI elements:
- Page title (
<title>
tag) - Subheader displaying the current page's title (such as Customers, Orders, Settings, etc.)
- Name of the currently logged-in user
- Some commonly used action buttons that apply to all pages in the system. Specifically, these buttons are
Show filters
,Export data to Excel
, andAdd new record
.
While managing the first two items can be achieved by detecting the current ui-router state (using its $stateChangeSuccess
event), handling the last two (username and buttons) poses some challenges, especially the buttons.
I can handle button actions using $broadcast
so that every controller is notified of clicks on any button. However, the complexity arises when different combinations of buttons are required for various pages - one page may need all buttons while another might not need any.
For example, when the CustomersController is loaded by ui-router, the MasterController receives the $stateChangeSuccess
event and automatically hides all buttons by default.
How can CustomersController communicate to MasterController that it requires two specific buttons from the start?
Theoretically, I could utilize $emit
from CustomersController to send an event to MasterController, but this approach seems somewhat inelegant. Events are typically used for signaling occurrences rather than making requests like "hey, MasterController, can you please display these buttons?".
Perhaps during the $stateChangeSuccess
event, I could somehow determine if there are any active listeners for my button click events and then hide buttons without attached listeners. However, I am uncertain how to implement this or whether it will function as intended - particularly concerning detachment of old listeners when ui-router refreshes the view with another controller.