view said.rhope @ 189:d0e3a13c1bd9 default tip

Remove old calculator example
author Mike Pavone <pavone@retrodev.com>
date Fri, 07 Oct 2011 00:24:04 -0700
parents f582fd6c75ee
children
line wrap: on
line source

/*
	This program implements Paul Graham's "Arc Challenge"
	While I think the Arc Challenge is a little silly, it does provide a simple 
	example of how to use the web framework I whipped up for the Rhope blog
*/

//The Import keyword causes another Rhope source file to be included
//Here we import the Rhope web framework
Import framework.rhope

//Here we define an event handler that will get called when the user clicks the button
//Event handlers get passed the current page tree and an event object
Save Text[page,event:out]
{
	//First we store the value from the text box in a session variable
	out <- [[[page]Set["said",[[page]Get Child By Name["foo"]]Value >>]
	//Then we empty the page object
	]Clear Children
	//And we add a hyperlink
	]Add Child[Web Link["Click Here","/said"]]
}

//This worker defines our page
Said[page,query:out]
{
	//Here we try to retrieve the session variable "said"
	[page]Index["said"]
	{
		//If it's present we display it on the page
		out <- [page]Add Child[ ["You said: "]Append[~] ]
	}{
		//Otherwise we provide them with a form to enter a word
		out <- [[[page]Add Child[Web Field["foo","","text"]]
			]Add Child[
				Web Button["blah","Click!"]
			//The Set Handler call here attaches the worker Save Text to the click event for the page
			//Events propagate can propagate themselves up the page hierarchy and handled wherever appropriate
			]]Set Handler["click",Save Text[?]]
	}
}

Main[args]
{
	[args]Index[1]
	{ port <- Int32[~] }
	{ port <- Val[80] }
	//Start Web starts the webserver and initializes the web framework
	Start Web[
		//It takes a dictionary mapping paths to Workers that define dynamic pages
		//Rather than just passing the name of the Worker, we're passing a List of info
		//that allows the framework to take care of some of the drudgework for us
		//The first element in the list is the worker name, the second the page title 
		//and the third element indicates whether this page will be using session data or not
		[Dictionary[]]Set["/said",[(0,"That's what she said",Yes)]Set[0,Said[?]]], port
	]
}