Determine If A Custom Event Has Been Subscribed To In jQuery

timelf123

New Member
I'm creating a web page using ASP.NET MVC. The page is fairly dynamic, so I usejQuery to make ajax calls to refresh partial views when necesary. One issue I'mrunning into is how to detect if a custom jQuery event has been bound or not.Here is my setup.Index.cshtml is the main page and only gets loaded once. It holds partialviews and is responsible for triggering initialize events.\[code\]<script type="text/javascript"> $(function () { $(document).trigger('initializeAddData'); $(document).trigger('initializeDisplayData'); });</script><div> @Html.Partial("AddData") @*Note: If there is no data in the database this partial view contains no markup.*@ @Html.Partial("DisplayData")</div>\[/code\]-\[code\]DisplayData.cshtml\[/code\] is a partial view that is displayed only when there is some data from the database to display. If there is no data in the database, a user may enter data and this view will be rendered to the client's browser after the database successfully adds new data.\[code\](function ($) { $(document).on('initializeDisplayData', initialize); function initialize() { // Initialize the partial view here; bind events, make ajax calls, etc. // ... }})(jQuery);\[/code\]-\[code\]AddData.cshtml\[/code\] is another partial view that adds data from user input to the database. If there is no data on the initial page load, this partial view needs to trigger the 'initializeDisplayData' event after the user enters some data. The JavaScript in this partial view needs to know if the \[code\]initializeDisplayData\[/code\] event has been subscribed to by the \[code\]AddData.cshtml\[/code\] partial view.\[code\]<script type="text/javascript"> $(function () { $(document).on('initializeAddData', initialize); function initialize() { // Initialize the partial view here; bind events, make ajax calls, etc. // ... $('#someButton').click(function { // Validate user input. // ... // Make an ajax call to save the user input. $.ajax({ type: 'POST', url: '@Url.Action("MyControllerMethod", "MyController")', data: { userData: userData } }).done(function (response) { // Refresh the markup in DisplayData.cshtml. // ... // Check if the 'initializeDisplayData' event has been subscribed to. If it has, trigger the event. if (how to check if 'initializeDisplayData' event has been subscribed to) { $(document).trigger('initializeDisplayData'); } }); }); } });</script>\[/code\]Using the above markup and JavaScript, how would I check if the\[code\]initializeDisplayData\[/code\] has been subscribed to or not in the \[code\]DisplayData.cshtml\[/code\]partial view?
 
Back
Top