views:

115

answers:

2

I've a couple of extension methods I've been developing for a couple of projects, they currently rely heavily on some AJAX to make bits and pieces work. The problem is that they require copying and pasting JavaScript files to the project you want to use it in.

As this JavaScript file only needs to be used once (all instances of the rendered control use the same file) I'd like to do something like add the script element to the headers collection of the page it's used on via a web-resource (embedding the file as a resource in the assembly). In Web-forms this wasn't a problem - you could add a script block to the headers with a specific ID and simply check for it on page load.

What's the MVC equivalent - is there an equivalent?

I'd like a solution that doesn't require the consumer to copy and paste/ add lines to pages or config...any thoughts?

+1  A: 

Here ya go, this guy wrote a custom FormlessScriptManager that will let you register scripts even when there is no <form runat="server"> in your page.

http://developmentalmadness.blogspot.com/2008/04/abstracting-systemwebuiscriptmanager.html

joshperry
Hi, good find - but unfortunately this would require the consumer to add more 'stuff' to the view than they would normally. Additionally, and as the author states, that solution won't work in a variety of scenarios.
Kieron
+1  A: 

Stephen Walther has some very good articles on MVC, including Html Helpers. http://weblogs.asp.net/stephenwalther.

A great place to see Html Helpler code is the MVC source code available at Codeplex.

There is a tutorial at www.asp.net/mvc on Html Helpers

Matthew
Stephen has a good tip (#45 - http://weblogs.asp.net/stephenwalther/archive/2008/10/08/asp-net-mvc-tip-45-use-client-view-data.aspx), this seems like it could be useful in solving the problem, but it's not quite there yet.
Kieron