The Golf Web Application Server home roadmap1.0 help rss

Hello, World: Basic Usage And The Golf Dev Environment

25 Dec 2009 - Micha Niskinsourcedownloaddemo

So, now it’s time to make a simple, “Hello, World” type of application to get started with Golf. The first thing is to create a directory in which to place all of the files that will make up your Golf app.

mkdir golf-example
cd golf-example

Okay, great. From now on we’ll refer to this directory as the app directory or approot.

Now, Golf apps are built using the MVC (Model-View-Controller) architecture. The absolute minimal Golf app is a bare controller. Normally, an app would have views and models to deal with, but since this is just a “Hello, World” example we’ll just hardcode everything into the controller.

A Simple Controller

The controller is defined in the special file controller.js, in the root of the app directory. This file is a JS source file, evaled in the global scope when the app is first loaded into the client.

Create the controller.js file with the following contents:

$.golf.controller = [
  { route: ".*",
    action: function(container, params) {
        "<h1>Hello, world! This is "+params[0]+"!</h1>"

Here you can see we created a sort of catchall route, with the route property set to the regex .*, matching any URI fragment and thus handling every request.

The Local Dev Server

Now let’s start up the Golf server in local dev mode. In your terminal go to the app directory and do:

golf .

Open a browser and go to http://localhost:4653/golf-example/#/home/. You should see something like this:

Disable JavaScript In Your Browser And Test The App

Notice how the application remains accessible, regardless of whether JS is enabled, or not. The JS proxy mechanism that makes this possible is discussed in depth in the Technical section of the docs.

blog comments powered by Disqus
Fork me on GitHub