开发者

When is document.ready fired when the script is loaded through the RederAction method?

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 J开发者_如何学运维obController.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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜