Toast

Note: Toast is still cooking, please report any bugs!

Introduction

Toast makes it simple to include local, remote and even inline content in your MVC application.
References can be added via:
  • Any Controller-based class
    [Content(InjectAt.HeadBottom, "/Scripts/BaseScript.js")]
    public class BaseController : Controller
    {
    }
  • Controller view methods
        [Content(InjectAt.HeadBottom, "/Scripts/Views.Home.js")]
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        [Content(InjectAt.Inline, "alert('Hi!')")]
        public PartialViewResult Partial()
        {
            return PartialView("APartialView");
        }
  • Common content references
        ToastCore.CommonContent.Add(GoogleAjaxLibraries.Get("jquery", "1.4.2"));
        ToastCore.CommonContent.Add(MultipleContent.UsingWildCardSearch("/Scripts/Common/*.js"));
        ToastCore.CommonContent.Add(Content.From("/Scripts/DemoScriptContent.js", CompressorFactory.Get<PackerCompression>()));
  • Automatic view inclusions
Adding a file that matches your view file name plus a .js extension will be included automatically
       Views
          - Index.aspx
          - Index.aspx.js  << Will be included 
          - LoginView.ascx
          - LoginView.ascx.js << Will be included 

Getting Started

Integrating Toast with your MVC application is as simple as referencing the ScriptBehind assembly in your application and adding a couple of lines to your global.asax.cs file.
   public class MvcApplication : HttpApplication
    {
        .......        

        public override void Init()
        {
            base.Init();
            ToastCore.Initialize(this);
        }
        .......
    }


Thats it! Your ready to go. This will configure Toast using all default settings including and using default script compression. You can then use any of the four methods of inclusion listed above.

Application Configuration

If you require more control over the Toast setup the options are available.

Script Compression

The script compression default is JsMin http://www.crockford.com/javascript/jsmin.html.
In version 0.2.* the following compression providers are available:
  • JsMin (JsMinCompression)
  • Packer (PackerCompression)
  • Google Closure (GoogleClosureCompression)
  • YUI Compressor (YUICompression)

To set the default compressor:
        ToastCore.Compression(x => x.SetDefaultFor<IJavaScriptContent, JsMinCompression>(CompressionLevel.High));

To add your own Compressor just create a class that implements ICompressionStrategy and use as above.

Registering application wide scripts

There are several ways to add a script or scripts that will be included in all requests.
  • Adding multiple local script files using wildcards
        ToastCore.CommonContent.Add(MultipleContent.UsingWildCardSearch("/Scripts/Common/*.js"));
  • Adding an embeded script
        ToastCore.CommonContent.Add(Content.From(
                                                                         @"function demoLog(msg) { $('#scriptsLoaded').append('<br>' + msg); }",
                                                                         "inline-added-script.js", InjectAt.HeadBottom));
  • Adding a reference to a remote script
        ToastCore.CommonContent.Add(Content.From("http://www.myurl.com/myscript.js"));
  • Adding a reference to a script hosted by a CDN, such as Google Ajax Libraries
        ToastCore.CommonContent.Add(GoogleAjaxLibraries.Get("jquery", "1.4.2"));

CSS Compression

...Coming soon...

Setting debug mode

By default Toasts SeperateFiles will match that of the applications build configuration, so when the application is in Debug build configuration Toast will be not combine any content files into a single file.
This can be override as follows:
        ToastCore.SeperateFile = false;


In debug mode scripts are not combined and are requested using friendly, human readable urls.

Putting it all together

  public class MvcApplication : HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" });
        }

        public override void Init()
        {
            base.Init();
            ToastCore.Initialize(this);
        }

        public void ConfigureScriptBehind()
        {
            // ToastCore.SeperateFiles = false;

            ToastCore.Compression(x => x.SetDefaultFor<IJavaScriptContent, JsMinCompression>(CompressionLevel.High));

            ToastCore.CommonContent(content =>
                {
                    content.Add(GoogleAjaxLibraries.Get("jquery", "1.4.2"));
                    content.Add(Content.From("/Scripts/DemoScriptContent.js", CompressorFactory.Get<PackerCompression>()));
                    content.Add(MultipleContent.UsingWildCardSearch("/Scripts/Common/*.js"));
                });
        }

        protected void Application_Start()
        {

            ConfigureScriptBehind();
            RegisterRoutes(RouteTable.Routes);
        }
    }

Last edited Apr 11, 2010 at 7:09 PM by Dve, version 5

Comments

No comments yet.