Model-View-Controller (MVC) is a pretty standard architecture/design pattern in software engineering which aims to separate business logic (i.e. what the system is modelling) from the display and both from control of the interactions between the two. AJAX is the name given to the idea of using JavaScript in a web page to retrieve data (usually in XML) from a web server without reloading the page.

An idea related to these two dropped into my head this afternoon, and I guess more than anything else I’m interested to know whether anyone else has thought about it (and possibly even implemented it. Specifically, the idea comes from the question ‘why should the Javascript be retrieving data to directly push into the model’?

My point, I guess, is that it would be nice to treat the web server as just another place that can ask the controller to operate on the model for it, rather than imagining the server as a source for data to be stuffed directly into the model. Now to be honest I’m not even aware of people thinking of JavaScript as the controller in a MVC arrangement, but I have no doubt plenty of people have. If you think of JavaScript as a real language, I thin it becomes quite obvious that a JavaScript application should implement a model, implement a controller for that model, and then handle the view by pushing the model out into the browser’s DOM.

So, how would we be able to implement this JavaScript controller given that the server has no way of pushing commands to it? The only real answer is that the JavaScript needs to poll the web server asking if it has any new commands. Let’s say, every 5 seconds, a JavaScript handler wakes up, and hits the server asking for any new actions the server wishes to perform. The server would then, I imagine, return an XML file describing the function calls (and parameters) the server would like to make (since the last request). JavaScript could then extract and call these functions, resulting in updates to the model and view as appropriate.

Polling is obviously not ideal, though the timing could be tuned to the specific application and could be varied with the frequency of server updates (i.e. if there haven’t been any updates in a while, check less often, if there are lots of updates, check more often). The question, I guess, is whether this is useful. At least for the time being, just grabbing some XML and slamming it into the model is a worthwhile optimisation. This is especially true now, since AJAX systems are, by software engineering standards, quite simple, but I imagine the day where a structured approach comes into it’s own is not too far away.

Now I just need to come up with a catchy name for the idea…