BeanCan Server

From RedBean

Jump to: navigation, search

The BeanCan Server

BeanCan is a php class that can act as a backend server for javascript centered web applications (JSON-RPC standard 2.0 compliant JSONRPC ). In a JS based web application your views and controllers are written in client-side Javascript while your models are still stored on the server. BeanCan acts as bridge between the client side javascript views and controllers on the one hand and the server side models on the other.

Here is an example demonstrating how to build a todo-application using the BeanCan architecture. First we include the redbean all-in-one package and setup a database connection using the setup() method. Next we create an instance of the BeanCan class and pass the JSON request if any. Here is the complete script:

<?php
require("rb.php"); 
R::setup();
class Model_Todo extends RedBean_SimpleModel {
public function getList() {
return R::findAndExport("todo");
}
}
$beancan = new RedBean_BeanCan;
if (isset($_POST["json"])) die($beancan->handleJSONRequest( $_POST["json"] ));
?>
<html>
<head><title>DEMO BEANCAN</title>
<script src="jq.js"></script>
</head>
<body>
<h1>My Todo List</h1>
<ul class="list">
<?php foreach(R::find("todo") as $todo): ?>
<li><button todo="<?php echo $todo->id; ?>">DONE</button><?php echo $todo->description; ?></li>
<?php endforeach; ?>
</ul>
<fieldset>
<label>Todo:<label>
<input type="text" id="dscr" />
<button>add to list</button>
</fieldset>
<script>
$("document").ready(function(){
//Delete an Item
var requestDelete = '{"jsonrpc":"2.0","id":"delete","method":"todo:trash","params":[@]}';
var clickHandler = function(){
var $btn = $(this);
$.post("?",{"json":requestDelete.replace("@",$btn.attr("todo"))},function(d){
eval("data="+d);
if (data.id=="delete") $btn.parent().remove();
});
}
//Attach Click Handlers
$("li button").click(clickHandler);
//Add an Item
$("fieldset button").click(function(){
var requestAdd =  '{"jsonrpc":"2.0","id":"add_todo","method":"todo:store","params":[{"description":"@"}]}';
$.post("?",{"json":requestAdd.replace("@",$("#dscr").val())},function(d){
eval(" data = "+d);
if (data.id=="add_todo") {
$(".list").append("<li><button todo='"+data.result+"'>DONE</button>"+$("#dscr").val()+"</li>");
//Restore Click Handlers
$("li button").unbind().click(clickHandler);
}
});		
});
});
</script>
</body>
</html>

Or download this script from the download section BeanCanDemo.tar.gz

Example

BeanCan makes use of FUSE. This means that you can send 4 types of commands to the BeanCan Server:


  1. load
  2. store
  3. trash
  4. custom


Requests 1-3 are handles automatically by RedBean. This means you can store/delete/load any bean automatically if you connect to the bean server without any effort. If you send an unrecognized command, FUSE tries to locate the model and passes the request.

The following request returns a page with ID 1:

{
"jsonrpc":"2.0",
"method":"page:load",
"params":[1],
"id":"myrequestid"
}


The following request creates a new page and returns its new ID:

{
"jsonrpc":"2.0",
"method":"page:store",
"params":[{"body":"lorem ipsum"}],
"id":"myrequestid"
}

The following request changes the text of page 2:

{
"jsonrpc":"2.0",
"method":"page:store",
"params":[{"body":"welcome","id":2}],
"id":"myrequestid"
}

This example request deletes page with ID 3:

{
"jsonrpc":"2.0",
"method":"page:trash",
"params":[3],
"id":"myrequestid"
}

And finally this request executes $page->mayAccess( $ip ) and returns the result. FUSE will connect automatically to the Model_Page class to accomplish this.

{
"jsonrpc":"2.0",
"method":"page:mayAccess",
"params":[ ipAddress ],
"id":"myrequestid"
}

The BeanCan server returns JSON reponses like this (created page and returns ID):

{
"jsonrpc":"2.0",
"result":"8",
"id":"myrequestid"
}

In case of an error:

{
"jsonrpc":"2.0",
"error":{"code":"-32603","message":"Invalid request"},
"id":"myrequestid"
}

Also see: Fuse,Observers

Personal tools