Async loaded partial view scripts initialization
On my control panel web page in ASP.NET MVC 3 site, a user can see his existing projects (its a site for designer portfolio), and can press a button to add a new project to the library.
Whenever a user click a button on a page, I retrieve the contents of the partial view and place them on the bottom of the page and call jQuery UI .dialog on a generated div to display a project editing/adding popup.
But how can I invoke a logic to initialize validation and other logic (click handlers for example) that is referencing elements in this partial view content? (Due to this I can't just initialize everything at document ready)
Do I just hardcode some function in a linked js (for example initProjectEditing()) and call it from the code that initializes the popup, or is there a more elegant way of doing it?
IMHO, the good way is to use Jquery widget . This thing allows you to define logic for your component in 1 place and then reuse it whenever you need.
You can create "add new project" Jquery widget.
Define all functionality concerning "add new project" in this widget (validation, event handlers, etc.)
After your "add new project" partial view was loaded and dialog was shown, just init "add new project" widget with loaded html.