-
Notifications
You must be signed in to change notification settings - Fork 66
MVC: Building MVC Applications
When building a Roxy MVC or Hybrid app, here's how the MVC pattern is used.
Controllers take user inputs, converts them into parameters to model functions. The controller also sends data to the view and can modify the default view or layout to use. Roxy provides controller helpers and request helpers.
Roxy controllers should generally be very thin: convert the HTTP parameters to model function parameters, and use ch:add-value() to send model function results to the view and layout.
In a document-oriented database like MarkLogic, the in-memory data structures and the way the data are stored are basically the same, so the impedance mismatch isn't a problem. Here, your models encapsulate related functionality. These library modules will be very much like the ones you use in other applications; in fact, you may be able to reuse code from other projects by putting it here.
Models consist of code for database interaction and business logic. When considering whether to put something in a model or in a view/layout, ask yourself whether you would need it regardless of the request format (HTML, XML, JSON, text). If the answer is yes, it probably goes here in the model. If the answer is no, it probably belongs in the view code for the format where it is needed.
An application will often need to respond to the same requests using different formats. By using views (and layouts), database interaction and business rules are separated from the presentation layer, so results can be returned as HTML, XML, JSON, or whatever format is needed. Roxy provides view helpers.
Roxy finds the right view for a request using a convention. Consider a URL: /tag/add.json. Roxy will call the add() function of the tag controller (/app/controllers/tag.xqy), and then call the JSON view: /app/views/tag/add.json.xqy. Using the scaffolding feature will create the view for you.
If you want to use a different view file, call the ch:use-view() function described in Controller Helpers from your controller function.
Applications frequently have parts of their user interface that stay the same across most of the application. For instance, the header and footer may stay basically constant, while the content between them is different on every page. The constant parts are the layout; the changing parts are the view.
You can set the default layout for your application by specifying the default layout option.
You might be able to use a single layout for your entire application, but sometimes you'll want more than one. You can override the default in a controller function by calling ch:use-layout().
This directory provides a home for reusable presentation-layer code. When you have a feature that appears on multiple pages, abstract it out and put it in a helper library.