Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

ASP.NET - scratching discs of code & entrepreneurship

Archive · View All
January 2012 8
April 2011 6
March 2011 4
August 2012 2
June 2011 2
May 2012 1
May 2011 1

John Katsiotis's Blog

RazorJS - Write Razor inside your Javascript files

Jun 14 2011 12:00AM by John Katsiotis   

Update: Full Source code is available at BitBucket

Many times I have a Javascript file that contains a link and in order to be more generic I would prefer writing something like ~/image.png rather than /image.png or /myapp/image.png.

Also when one of my Views in an MVC project interacts with 3rd party components and services (like Google Maps, UserVoice etc.) contains a lot of Javascript code along with html/server-side code. The minute that this page reaches a stable version I tend to put the javascript code in a separate file to keep things cleaner. But if that code depends on my Model and is being rendered by Razor I can’t do that. That means this code must stay on that view and I may end up with duplicating it to other pages with similar functionality.

Struggling with the above I ran into a very good Nuget Package called RazorEngine and thought to create something using that to be able to have more flexibility in my Javascript Code.

And the name of this is RazorJS.

Let’s see a few examples:

Javascript Code in an external file

The JS File

image

The Razor Code

image

The Rendered HTML

image

Javascript Code with Model dependency

The JavaScript File

image

The Code

image

The HTML

image

What about performance?

The JS files are being cached just like Razor files with a CacheDependency so if a file changes the Razor template is being updated instantly. Every file is compiled just like Razor Pages and is being compiled the first time is requested or when a change is detected.

So the time to render each file the second time (and every time after) is really really small.

Known Issues

  • For now the Nuget package doesn’t have a dependency to RazorEngine because it needs a later version to perform better which isn’t yet available. Once RazorEngine 2.2 is released I will update the package.
  • For now the only method available to call is Href and the only property is only two properties are Model and Url. Please let me know other methods or properties that could be useful when writing Javascript.

That’s all for now! You can find it at the Nuget Feed!

Cheers!


Republished from djsolid - scratching discs of code & entrepreneurship [19 clicks].  Read the original version here [4 clicks].

John Katsiotis
988 · 0% · 26
1
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"RazorJS - Write Razor inside your Javascript files" rated 5 out of 5 by 1 readers
RazorJS - Write Razor inside your Javascript files , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]