Created
September 15, 2011 16:44
-
-
Save jonathascosta/1219758 to your computer and use it in GitHub Desktop.
ActionResult with Javascript for a Ext.Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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