Trying to improve my coding styles I've tried different solutions but I can't figure out what is the best.
I've started putting JavaScript inside my views but I don't particularly like this solution.
It's hard to debug with Visual Studio, and it kinds of "pollutes" the page.
My new "trend" is to put the scripts for the page in a separate file.
The only problem I am facing is with the code.
To solve the problem I've defined JavaScript variables like this:
<script type="text/javascript"> var PriceListFetchAction = '<%=Url.Action("Fetch", "PriceList")%>'; var UploaderAction = '<%=Url.Action("UploadExcelPriceList", "PriceList")%>'; var ModelId = '<%=Model.id%>'; var ImportType = '<%=Model.Type%>'; var customerCodeFetchAction = '<%=Url.Action("FetchByCustomerCode", "Customers")%>'; var customerNameFetchAction = '<%=Url.Action("FetchByCustomerName", "Customers")%>'; var ImportErpAction = '<%=Url.Action("ImportPriceListErp", "PriceList")%>'; var imageCalendar = '<%=Url.Content("~/Content/Images/calendar.png")%>'; </script>
and then I use the variables in my JavaScript file. What is the best in terms of performance, debugging, style for you?
cshtml is included in every view in the web project, it is a good place to include script references that implement functionality for page elements, such as the navbar found included on every page. Most of the JavaScript libraries are rendered at the bottom of the <body> element of _Layout.
You should put your JS code in a separate file because this makes it easier to test and develop. The question of how you serve the code is a different matter. Serving the HTML and the JS separately has the advantage that a client can cache the JS.
JavaScript can be used in asp.net mvc. If you go for Asp.NET Web forms, there is no need to have a detailed understanding of HTML, CSS or JavaScript as it abstracts all these details and provides automatic plumbing.
The recommended approach is to put in a separate JavaScript file or inside a section defined in Layout page. A section can be added in the MVC Layout page using @RenderSection() directive. For example, we can define a section in Layout page under <head> tag for scripts like below.
I follow a handful of rules:
So, here's an example:
I'll add jQuery and jQuery metadata to my project: http://plugins.jquery.com/project/metadata
Then, in my master js file, I'll extend jQuery with my own namespace:
$.extend({ fatDish : { url : {}, urls : function(a) { $.extend($.fatDish.url, a); } } });
Almost all of my customized js logic will live in the $.fatDish namespace.
Now, let's say I want to pass a MVC route to $.fatDish. In my aspx page, I'd write the following:
<script src="@Url.Content("~/path/master.js")" type="text/javascript"></script> <script type="text/javascript"> $.fatDish.urls({ path1 : '@Url.Action("Index", "Home")' }); </script>
In a js file, I can now write:
window.location = $.fatDish.url.path1;
A second approach is to use jQuery metadata (which I mentioned above). On your aspx page, you could write something like:
<div class="faux-link {act:'@Url.Action("Index", "Home")'}">Go Somewhere</div>
Then, in your js file, you can grab the route value like so:
$('.faux-link').click(function() { var $this = $(this); var href = $this.metadata().act; window.location = href; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With