I am trying to create a reusable widget in ASP.Net MVC. This is a widget that concatenates 2 dates into a string. The widget contains 3 files: Widget.cshtml, Widget.css, and Widget.js and dependencies to several jquery libraries.
Widget.cshtml
<h2>Create a date range</h2>
<div>
<label for="startDate">Start:</label>
<input id="startDate" class="date" name="startDate" type="text" />
</div>
<div>
<label for="endDate">End:</label>
<input id="endDate" class="date" name="endDate" type="text" />
</div>
<p id="output"></p>
<button id="createDateRange">Create</button>
Widget.css
label { color: #555;}
Widget.js
$(function () {
$(".date").datepicker();
$("#createDateRange").click(function () {
var start = $("#startDate").val();
var end = $("#endDate").val();
$("#output").html(start + " to " + end);
});
});
I want to make this widget reusable in my MVC pages. I have come up with several options, including:
- Create an HTML Helper
- RenderPartial
- RenderAction
- Remove all css and javascript dependencies from the widget file. Manually Copy-And-Paste into every view that I'm using it in the correct place.
I was about to use a helper, when I remembered Steve Souders rules for web site performance:
Rule 5: Put stylesheets at the top
Rule 6: Put scripts at the bottom
Options 1), 2) and 3) all seem like good ideas, but they all write the code inline and violate the performance rules. Option 4) allows me to manually put the javascript and css files where they belong, but copy-and-paste is prone to mistakes. A potential idea would be write a scaffolder that puts everything where it belongs, but that seems too much work for right now.
What is the best (hopefully simple) way to do widgets and still follow performance rules 5 & 6?