Skip to content

How to bind a model function and a view

macourtney edited this page Sep 13, 2010 · 3 revisions

Added for version: 0.6
Updated for version: 0.7

Bindings are used to bind data from one or more models to a view. To generate a binding see: How to generate a binding. In a previous version of Conjure, the controller was responsible for passing data from the model to the view. However, since controllers already also do some basic routing (redirecting to other actions), it made sense to move binding to its own file.

Once binding was moved to it’s own file, it became possible to reuse bindings just like you could reuse views.

Calling Bindings

For example, lets say you have an action called “hello-world” in controller “hello” which does not pull any data from a model. You can use the direct.clj binding under templates.

The hello-world action in your hello controller would look something like:

(def-action hello-world
  (bind-by-controller-action "templates" "direct" []))

Note, in the above example, the direct binding uses the request-map to determine the controller and action of the view to display.

If you want to use the binding for the controller and action in the request-map, simply use:

(def-action hello-world
  (bind))

Passing a Parameter to a View.

Lets assume our hello-world view takes a message to display as a parameter. In our binding, we can pass the message to our view in our binding definition like so:

(def-binding []
  (render-view "Hello World!"))

The above binding definition which should be found in your hello_world.clj binding file, simply passes “Hello World!” as the message to display to our view.

Passing a Record from a Model

Lets say you have a message model containing all of the messages you wish to display. We want to allow the user to select the id of the message to display.

We can simply pass the id as a parameter in the request-map. Then, we pull the message with the given id from the model, and display the text of the message.

Our new binding definition looks like:

(def-binding []
  (let [id (conjure.core.server.request/id-str)]
    (render-view (:text (message/get-record id)))))

The above binding definition does not handle errors for simplicities sake.

Clone this wiki locally