andrewdavey / cassette

Manages .NET web application assets (scripts, css and templates)
http://getcassette.net
MIT License
536 stars 144 forks source link

Thoughts on js.cshtml and possibly css.cshtml #72

Closed dotnetchris closed 12 years ago

dotnetchris commented 13 years ago

What are your thoughts on writing javascript inside of Razor views, and even potentially css?

I find there are many times this would be extremely beneficial as this would allow access to Url.Action() and similar methods.

There also might be times where it could be beneficial to actually generate javascript code using razor through looping / logic statements, a clear use case for this could be doing something different in your javascript for debug vs production that you wouldn't want to be sent across the wire as it would if you did it solely in javascript with branching logic based on environment.

A feature like this could integrate very well with Cassette that during it's compilation processes it executes the razor views and then stores the resulting JS.

I have thought about these capabilities for the past few months but then continuously thought about the cost associated with them that it would be very hard if not impossible to integrate with combination/minification sanely.

I also feel these files would be great to work along side with the concepts of modules. Seeing as Cassette effectively brings the AMD (asynchronous module definition) functionality that is part of node.js/NPN (npm?) that brings some sanity to the structuring of javascript code.

andrewdavey commented 12 years ago

A common use case I have is passing server-side data into a script for use client-side. This is now possible using Assets.Scripts.AddPageData as seen here: https://github.com/andrewdavey/cassette/issues/66#issuecomment-2115605 There is also an AddInline method to inject any script you like into the page.

Switching between different script/css, based on the environment, could be done in the Cassette configuration code. You just need to specify which module/assets to add based on some condition. Let me know if you need some specific advice for this.

Generating script/css using a Razor view is feasible. For Cassette to do this we'd probably need to use the RazorEngine project http://razorengine.codeplex.com/ since we need to run the views outside of a normal page request cycle.

However, if the actual use case is simply switching between different files to include, then I think using the Cassette configuration is a simpler approach.

dotnetchris commented 12 years ago

That AddPageData could possibly handle all of the use cases I could think of aside from doing code generation of javascript itself which I think I only ever really wished I could do once.