Ok, so my project was developed using PHP with jQuery Mobile as primary “view”. Since a few people asked for an example, I translated the idea into a Xojo project. After looking at the MVC example in Beatrix’s project I’ve come to realize there is probably lots of different ways to implement MVC in your projects, and you’ll have to decide on a way that is most comfortable for you.
This is my first attempt at MVC in Xojo, so please chirp in if you spot improvements that can be made. First, here is a diagram of how I envision the MVC process…
The goal of MVC is to separate your data from your view (presentation). All the user interaction is handled by the controller. Models are basically your data sources, and views are your presentation part of the system.
The controller retrieves data from the model, and then uses a view object to render this data into a presentable format (e.g. HTML). So in what format is data passed between the model and the view? I prefer to use JSON because it is lightweight, easy to work with, and can be extended without breaking compatibility with existing models and views.
So, onto a practical Xojo example… you can download the Xojo project here.
From the combo-boxes simply select the model and view you want to use and then click the “List Products” button. The output is rendered as text into a TextArea control. So what is the advantage of writing the program this way?
You can now add new models and views, without affecting the rest of the program. For example, let’s assume a new exciting database, named “AwesomeDB”, enters the market. All you have to do to make your program compatible with AwesomeDB is to write a new AwesomeDB model. Your views are unaffected and doesn’t even know about the new database addition.
And vesa versa… let’s say you currently output your view as HTML, but decided that you also want to output it as a PDF report… all you have to do is build a new PDFView object, and your models doesn’t even know about the fact that the data they provide are rendered as PDF documents.
Hope this helps those new to MVC. I’m still learning myself about how to use the MVC design pattern in my projects.