views:

25

answers:

2

I have an action that summarizes an entity's information. The view for the action contains a script to handle the summary's UI. I also have another action that loads this view multiple times (to summarize multiple entity instances). It seems that the jQuery document.ready function fires each time the action's view is loaded on the page. Is that correct behavior?

For example:

I have a SummaryController.Summarize(Entity item) action which has the script to do all the fancy UI stuff. I also have a JobController.Index() action that passes 5 Entity instances to it's view. The view calls RenderAction("Summarize", "Summary", entity) to display each entity. However, I'm expecting the document.ready function to only fire once all of the Summarize actions have loaded (to link up events) but it fires each time, so my events are subscribed to multiple times. Hope that makes sense.

+1  A: 

The RenderAction method outputs the raw HTML returned by the action.
The browser is unaware that the HTML came from different actions.

Therefore, each call to $(document).ready will execute separately.

SLaks
A: 

The HTML is assembled before it is sended to the client. So the "final" page is sended with each controls in it (rendered via RenderAction).

So each of the user controls rendered in the page will have its own snippet of js code.

document.ready registers an handler to be called when the document is ready. Each time you call the method, a new handler is attached. That's why it is fired more than once.

Hope this helps

Mike Gleason jr Couturier