Skip to content

Instantly share code, notes, and snippets.

@jonathascosta
Created September 15, 2011 16:44
Show Gist options
  • Save jonathascosta/1219758 to your computer and use it in GitHub Desktop.
Save jonathascosta/1219758 to your computer and use it in GitHub Desktop.
ActionResult with Javascript for a Ext.Store
using System.Web.Mvc;
using Newtonsoft.Json;
namespace ActionResults
{
public class ExtStoreResult : ActionResult
{
public virtual string StoreName { get; set; }
public virtual object StoreData { get; set; }
// TODO: Use a decent template engine
private const string extStoreTemplate =
@"Ext.define('{0}', {{
extend: 'Ext.data.Store',
data: {1}
}});";
public ExtStoreResult(string storeName, object storeData)
{
StoreName = storeName;
StoreData = storeData;
}
public override void ExecuteResult(ControllerContext context)
{
var extStore = runTemplate(extStoreTemplate, StoreName, JsonConvert.SerializeObject(StoreData));
var response = context.HttpContext.Response;
response.ContentType = "text/javascript";
response.Write(extStore);
}
private string runTemplate(string template, params object[] args)
{
return string.Format(template, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment