views:

59

answers:

2

Hi,

I'm using the ASP.NET MVC Framework, jQuery and Ajax calls to do some Partial view rendering based on client input, my issue is that some of the scripts requires by the partial view rely on the ready event, raised by jQuery.

The scripts, both external, and on in the view are being loaded without issue, but as the page is already loaded, the ready event is never fired.

Is there a way to pre-process the scripts (loaded using $.ajax with it's async property set to false) and do something to get the ready event to fire?

Either that or should be looking a way to inform the main page that there's a method that needs to be run, in other words create a framework for calling initialisation methods dynamically when they're pulled from the server using Ajax and work with the ready event for when they come down as part of the page?

Thanks,

Kieron

A: 

Do you need the ready event to fire because you need to attach behaviors to what you just loaded?

If so you could use either the .live() function or the livequery plugin.

With both you can just predefine the behavior based on a selector whether or not the things matching the selector exist or not. When matching items are loaded they will automatically assume the behavior.

For something more complex the livequery plugin will fire an event letting you know that something new matching your selectors has been added, letting you run any code you need. This will let you keep all the relevant code in one .js file instead of having spread between a bunch of partial views.

The built in live() function: http://api.jquery.com/live/

The livequery plugin: http://plugins.jquery.com/project/livequery/

jwsample