I need to develop a generic jQuery-based search plugin for the ASP.NET MVC application I’m building, but I can’t figure out how it’s supposed to fit, or what the best practice is. I want to do the following:
$().ready(function() { $('#searchHolder').customSearch('MyApp.Models.User'); });
As long as I have implemented a specific interface on Models.User, jQuery will be able to talk to a reflection service to generically construct the relevant UI.
Sounds fun, but it seems that I’m now calling the JavaScript from the View, which is in turn going to do some View-related activity to build the search UI, and then to do the search and interact with the user it’s going to throw a bunch of Controller tasks in there.
So where does this really fit? Is there a different way I can structure my jQuery plugin so that it conforms more to the idea of MVC? Does MVC work when it scales down to its own form within another MVC structure? Should I just ignore these issues for the sake of one plugin?
Just to follow up (I’m very surprised nobody else has had any opinions on this), in an effort to keep best practice I’ve opted to adopt jTemplates.
It enables me to request some Model-style JSON from my server-side Controller and process it using syntax similar to that I would already use in a View, which now keeps any required JavaScript UI MVC-compatible. There’s a small overhead in that the client will need to request the View template from the server, but if that becomes too slow I can always sacrifice a little and send it over with the initial JSON request.