PDA

View Full Version : MVC and multiple XML files



daidai
December 17th, 2008, 10:09 AM
Hi all
Im trying to work out how to load separate XML files for each section of the site, which is built with MVC. I didn't write the code, so this is already tough - the site is heavy and I've been asked to lighten the sites load time by splitting up the XML loads, by chopping up the single XML file into several individual XML files.
My problem is that I cant work out the best way to achieve this. At present the Document class loads in the XML. This then creates an instance of the Model class with the XML data.
My problem is that I can't work out the best way to tell the Model class to load a different XML file each time the Navigation View class is clicked, as there is no connection back from the Model class to the Document Class.
dai2
:p:

creatify
December 17th, 2008, 02:20 PM
A lot of it depends on how the MVC is set up. You can load the initial xml file in your doc class if that file is basically a config file. Then, in theory, when you click a nav item that nav item should send an event to the controller - you could set up an xmlparser class that would listen to the nav clicked, then load the appropriate XML doc, upon load, the xmlparser could then push the xml to the model to be stored, then the model would notify the views that that section's xml has successfully loaded. Then the view would grab the data and display it.

One tricky thing that always gets people is how to let the controller know which nav button was pressed in order to load the appropriate item. This can be accomplished in various ways. Usually, in the config.xml, you have some structure that relates directly to the nav items, in fact, I'd place the sub-page xml paths in the config and then figure a way to bind those paths with the nav.

Hope this helps.

daidai
January 7th, 2009, 04:56 AM
apologies creatify I never thanked you for your reply, this project has been on hold for xmas, now im back on it
thanks again

Joony5
January 7th, 2009, 09:47 AM
A DAO (http://en.wikipedia.org/wiki/Data_Access_Object) might be in order.

The DAO is just a class that holds all the code for loading the config data. Once everything is loaded you can process the config data, merge everything, do whatever, and when you're ready you can dispatch an event with the config data attached.

So all you do is get the model to create an instance of the configDAO, and then add an event listener for the DAO's complete event. Once the event is triggered you've got all the config data you need.

It adds a level of abstraction, so you can test the model with any old DAO. For example, while developing offline you could have a simple test DAO that has hard-coded config data. This is also great for unit testing. Or, you could change the DAO to get the data from a webservice without having to dig deap in to other code.

Creatify is right about getting the View to direct the nav button event to the Controller, have the Controller pass a variable to a generic sectionDAO telling is which section to load, then pass it to the model to store when it's ready. The Model should then have a method to notify the View that something is loading/has loaded.

Hope this helps.