Generating MVC View Models from Arrays Generated by jQuery
Many times on my latest work-for-hire project Iâve been reluctantly adding a new MVC View Model to the build. I donât want to be one of those developers slapping redundant clutter out of ignorance and the fear of being perceived of as âtoo slow.â And it is time consuming to write a View Modelâespecially for an HTML form with several dozen data points.
What makes me feel just a little bit better is the fact that a View Model needs to be redundant for these very powerful reasons:
- The View Model often flattens hierarchical relationships on the server among several entities/tables. Remembering this literally puts the View back into View Model.
- The View Model is a strongly-typed representation of what can be generalized into a big bag of name-value pairs. My assertion here is that it is easier to maintain the strongly-typed object than to painstakingly account for several dozen strings in a bag.
Whatâs even better for me is this little JavaScript utility function:
$.rx.utilityGetInputProperties = function (selector, context) {
var props = [];
$(selector, context)
.find('input, select, textarea')
.each(function () {
props.push($(this).attr('id'));
});
return _.sortBy(props, function (p) { return p; });
};
This jQuery function will allow me to generate POCO C# View Model classes with properties named after the id attribute of HTML form elements quite quickly. There are several âpeculiarâ conventions from my little world here:
- I am biased toward using AJAX/JSON to post HTML form dataâthis means I can use the
idattribute of HTML form elements. - My IDs for HTML forms are in Pascal caseâwhich can be quite annoying to JavaScript-centered developers. Worse, is me deliberately doing this for CSS class names as well!
Using $(document).ajaxComplete() as a Secondary Initialization âHookâ for Client Scripts
Getting JavaScript loaded and running is ridiculously difficult. In very large, complex clients $(document).ready() is not enough. My intent as a programmer is to compose logic into discrete units that are as independent as possible. This makes maintaining the code easier and it allows new developers to not freak out with the fear of breaking something somewhere in the mysterious deep⊠Without something like $(document).ajaxComplete(), the level of independence would decreaseâas I would be tempted to pack more and more dependencies into the $(document).ready() function block as the client got larger and larger. This is what my $(document).ajaxComplete() pattern looks like today:
$(document).ajaxComplete(function (event, xhr, options) {
if (options.url.indexOf('/MyMvcRoute') !== -1) {
//Visuals for each panel:
$.rx.panels();
$.rx.panelOne();
$.rx.panelTwo();
$.rx.panelThree();
});
}
});
Related Links
-
Underscore.js: âUnderscore provides 60-odd functions that support both the usual functional suspects: map, select, invoke â as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in functions, if present, so modern browsers will use the native implementations of
forEach, map, reduce, filter, every, some andindexOf.â - âHTML5 JSON Report format: tool to view any JSON data in a human-readable HTMLâ
- âThe Cicada Principle and Why It Matters to Web Designersâ
- âClever use of HTML5â via @sambrown
- HTML5 Shiv: âSjoerd Visscher mentioned an interesting technique that could be used to force IE to use the specified CSS styling. Specifically, once you create a new DOM element (of the same name as the one in the document) all styling is applied. You don’t even have to inject the element in order for this to occur.â
-
Exploring Globalization with jQuery: âOne file
jQuery.glob.all.jsincludes around 350 cultures currently. So with this we need to include only this file for all the cultures supported by the plugin.â - jQuery 1.5 Released: âEasily the largest change in this release is the complete rewrite of the Ajax module in jQuery. This rewrite helps to fix a lot of gaps that existed in the old Ajax system along with providing a higher level of consistency across the API.â



