Skip to content

Instantly share code, notes, and snippets.

@wfaler
wfaler / widgetresult.html
Created January 17, 2011 22:59
widgetresult.html
<b>Id</b><br/>
1<br/>
<b>Name</b><br/>
My Widget<br/>
<b>Price</b><br/>
10000<br/>
@wfaler
wfaler / json.js
Created January 17, 2011 23:35
json.js
$.ajax({
beforeSend: function(req) {
req.setRequestHeader("Accept", "application/json");
},
type:"GET",
url: url,
success: function(data){
alert("JSON View=" + data);
}
});
@wfaler
wfaler / widget.json
Created January 17, 2011 23:38
widget.json
{"id":1, "name":"My Widget", "priceInPence":10000}
@wfaler
wfaler / defaultlayout.html
Created January 17, 2011 23:44
defaultlayout.html
<html>
<head><title>Bowler QuickStart App</title></head>
<body>
<h2>This is from the default.mustache layout</h2>
<div>
{{&doLayout}}
</div>
</body>
</html>
@wfaler
wfaler / parentlayout.scala
Created January 17, 2011 23:52
parentlayout.scala
// parent layout, that uses a LayoutModel to enrich the layout based on request if needed.
val parentLayout = DefaultLayout("default", "doLayout", None, new ParentLayoutModel)
// this is a childLayout for parentLayout, and has the parent set on it, as shown.
val composableLayout = DefaultLayout("child", "doLayout", Some(parentLayout))
@wfaler
wfaler / advancedlayoutmodel.scala
Created January 17, 2011 23:58
advancedlayoutmodel.scala
class ParentLayoutModel extends LayoutModel{
def model(request: Request, viewModel: Map[String, Any], childView: String): Map[String, Any] = {
val map = new HashMap[String, Any]
// lets start by adding the childView so it's guaranteed to appear!
map += "doLayout" -> childView
// lets see if we want to add a tab panel to this layout!
val regex = new Regex("^.*/composable/.*$")
if(regex.pattern.matcher(request.getPath).matches){
// using a raw Scalate Template Engine with absolute URI to get and render the template.
@wfaler
wfaler / post.scala
Created January 18, 2011 00:19
post.scala
class MyController extends Controller with ParameterMapper{
post("/widgets/:id")((request, response) => {
mapRequest[Widget](request)(widget => {
// update the widget
Widgets.update(widget)
response.sendRedirect("/widgets")
})
})
@wfaler
wfaler / OptionMapper.scala
Created January 18, 2011 00:33
OptionMapper.scala
def `GET /widgets/:id`(widget: Option[Widget]) = render(widget)
@wfaler
wfaler / WidgetTransformer.scala
Created January 18, 2011 00:39
WidgetTransformer.scala
/**
* Transforms from single request parameters to an object, for instance in this case from a Widget ID
* to an Actual Widget, or returns None if a transformation cannot be done.<br/>
* Must be registered with the TransformerRegistry, as done in the bootstrap above.
*/
class WidgetTransformer extends StringValueTransformer[Widget]{
def toValue(from: String): Option[Widget] = Widgets.find(Integer.parseInt(from))
}
@wfaler
wfaler / TransformerRegistry.scala
Created January 18, 2011 00:40
TransformerRegistry.scala
// Register the WidgetTransformer so that we can look up Widgets for pages by ID
TransformerRegistry.registerTransformer(classOf[Widget], classOf[WidgetTransformer])