Bespoke Software Solutions in C#.Net, ASP.Net MVC, JQuery, MEF and more
Posts tagged mvc
ASP.NET MVC JQuery ScriptManager
Oct 23rd
As I was developing a CMS system for a client, we had a very unique scenario (well maybe not that unique) where we had custom “parts” which you could add to a page. The “parts” were highly customisable, thus you could have 3 instances of the same “part” on one page. The html and javascript rendered by the part would conflict with each other, because they had the same id’s and registered events.
I needed something similar to the old ASP.NET Script manager and the way that old ASP.NET used the “clientId” instead of the “htmlId” when dealing with html objects. The “clientId” was a generated unique key that prevented duplicated user controls conflicting with one another.
I found quite a few implementations of ScriptManager (http://pietschsoft.com/post/2009/08/13/Simple-ScriptManager-for-ASPNET-MVC.aspx, http://aspmvccombine.codeplex.com/) for MVC, but none of them were solving my unique “htmlId” problem. If you are looking for a ScriptManager for MVC, then the two solutions I provided would be more than enough. If, however, you are in a similar situation as I am with the html controls, please read on…
I decided to combine knowledge obtained from the two ScriptManager implementations, and create my own light-weight solution addressing both problems.
Because I wanted the usage of this to be very simple, I created a fluent interface which I can use in all my parts…
This is what the end result looks like:
<% Html.ScriptManager()
.CreateClientScript(GetUniqueId())
.AddParam("SelectId", GetUniqueId("selectList"))
.AddParam("SourceUrl", Url.Action("Index", "Person"))
.Ready(() => {%>
$.getJSON(params.SourceUrl, function(result) {
$.populate_combobox(result, params.SelectId);
});
<%});%>
<% Html.ScriptManager().AddStaticMethod("populate_combobox", () =>{ %>
jQuery.populate_combobox = function(data, selectId) {
$.each(data, function() {
var option = new Option(this.text, this.value);
var dropdownList = $(selectId)[0];
if ($.browser.msie) {
dropdownList.add(option);
} else {
dropdownList.add(option, null);
}
});
}
<%});%>
<div id="<%=GetUniqueId() %>_part">
<select id="<%=GetUniqueId("selectList") %>"></select>
</div>
My “GetUniqueId()” function returns a combination of the PartId, PageId, but you can replace this with anything you fancy.
The javascript that gets generated by this is:
var ContentPart_AboutPage_82 = {
Init: function(params) {
$.getJSON(params.SourceUrl, function(result) {
$.populate_combobox(result, params.SelectId);
});
}
}
var ContentPart_AboutPage_83 = {
Init: function(params) {
$.getJSON(params.SourceUrl, function(result) {
$.populate_combobox(result, params.SelectId);
});
}
}
var ContentPart_AboutPage_84 = {
Init: function(params) {
$.getJSON(params.SourceUrl, function(result) {
$.populate_combobox(result, params.SelectId);
});
}
}
$(document).ready(function() {
ContentPart_AboutPage_82.Init({
SelectId:'ContentPart_AboutPage_82_selectList',
SourceUrl:'/Person'
});
ContentPart_AboutPage_83.Init({
SelectId:'ContentPart_AboutPage_83_selectList',
SourceUrl:'/Person'
});
ContentPart_AboutPage_84.Init({
SelectId:'ContentPart_AboutPage_84_selectList',
SourceUrl:'/Person'
});
});
jQuery.populate_combobox = function(data, selectId) {
$.each(data, function() {
var option = new Option(this.text, this.value);
var dropdownList = $(selectId)[0];
if ($.browser.msie) {
dropdownList.add(option);
} else {
dropdownList.add(option, null);
}
});
}
Update: I have gone even further and added the concept of “SharedVariables” to the ScriptManager where the “SourceUrl” above and even the Json request is fired only once!
Here is my ScriptManager Class:
public class ScriptManager
{
private readonly HtmlHelper _helper;
public IDictionary<string, ClientScript> ClientScripts
{
get
{
if (_helper.ViewContext.HttpContext.Items["ClientScripts"] == null)
_helper.ViewContext.HttpContext.Items["ClientScripts"] = new Dictionary<string, ClientScript>();
return (IDictionary<string, ClientScript>)_helper.ViewContext.HttpContext.Items["ClientScripts"];
}
}
public IDictionary<string, Action> Methods
{
get
{
if (_helper.ViewContext.HttpContext.Items["ScriptMethods"] == null)
_helper.ViewContext.HttpContext.Items["ScriptMethods"] = new Dictionary<string, Action>();
return (IDictionary<string, Action>) _helper.ViewContext.HttpContext.Items["ScriptMethods"];
}
}
public ScriptManager(HtmlHelper helper)
{
_helper = helper;
}
public ClientScript CreateClientScript(string key)
{
if(!ClientScripts.ContainsKey(key))
ClientScripts.Add(key, new ClientScript());
return ClientScripts[key];
}
public ScriptManager AddStaticMethod(string key, Action javascript)
{
if(!Methods.ContainsKey(key))
Methods.Add(key, javascript);
return this;
}
public void Render()
{
TextWriter writer = _helper.ViewContext.HttpContext.Response.Output;
writer.WriteLine("<script type=\"text/javascript\">");
foreach (var clientScript in ClientScripts.Keys)
{
writer.WriteLine("var " + clientScript + " = {");
writer.WriteLine(" Init: function(params) {");
ClientScripts[clientScript].Value();
writer.WriteLine(" }");
writer.WriteLine("}");
}
writer.WriteLine("$(document).ready(function() {");
foreach(var clientScript in ClientScripts.Keys)
{
var client = ClientScripts[clientScript];
writer.WriteLine(" " + clientScript + ".Init({");
foreach (var param in client.Params)
writer.WriteLine(" {0}:'{1}'{2}", param.Key, param.Value, IsLast(client.Params, param.Key) ? string.Empty : ",");
writer.WriteLine(" });");
}
writer.WriteLine("});");
foreach(var method in Methods.Values)
method();
writer.WriteLine("</script>");
}
private static bool IsLast(IDictionary<string, string> parameters, string key)
{
var keys = parameters.Keys.ToList();
return keys.Count > 0 && keys[keys.Count-1] == key;
}
}
Here is the ClientScript class:
public class ClientScript
{
private readonly IDictionary<string, string> _parameters = new Dictionary<string, string>();
private Action _script;
public IDictionary<string, string> Params
{
get { return _parameters; }
}
public ClientScript AddParam(string key, string value)
{
Params.Add(key, value);
return this;
}
public ClientScript Ready(Action script)
{
_script = script;
return this;
}
public Action Value
{
get
{
return _script;
}
}
}
Here is my extension method:
public static ScriptManager ScriptManager(this HtmlHelper helper)
{
return new ScriptManager(helper);
}
And then you only need to add this at the end of your master page:
<% Html.ScriptManager().Render(); %>