官网:http://www.chicagoboss.org/
下载:https://github.com/evanmiller/ChicagoBoss/
代码:
1. 创建工程:
git clone https://github.com/evanmiller/ChicagoBoss.git
cd ChicagoBoss
make
make app PROJECT=myapp
cd ../myapp
./init-dev.sh
2. src/controller/myapp_greeting_controller.erl
-module(myapp_greeting_controller, [Req]).
-compile(export_all).
hello('GET', []) ->
{ok, [{greeting, "Hello World!"}]}.
list('GET', []) ->
Greetings = boss_db:find(greeting, []),
{ok, [{greetings, Greetings}]}.
create('GET', []) ->
ok;
create('POST', []) ->
GreetingText = Req:post_param("greeting_text"),
NewGreeting = greeting:new(id, GreetingText),
%{ok, SavedGreeting} = NewGreeting:save(),
%{redirect, [{action, "list"}]}.
case NewGreeting:save() of
{ok, SavedGreeting} ->
{redirect, [{action, "list"}]};
{error, ErrorList} ->
{ok, [{errors, ErrorList}, {new_msg, NewGreeting}]}
end.
goodbye('POST', []) ->
boss_db:delete(Req:post_param("greeting_id")),
{redirect, [{action, "list"}]}.
send_test_message('GET', []) ->
TestMessage = "Free at last!",
boss_mq:push("test-channel", TestMessage),
{output, TestMessage}.
pull('GET', [LastTimestamp]) ->
{ok, Timestamp, Greetings} = boss_mq:pull("new-greetings", list_to_integer(LastTimestamp)),
{json, [{timestamp, Timestamp}, {greetings, Greetings}]}.
live('GET', []) ->
Greetings = boss_db:find(greeting, []),
Timestamp = boss_mq:now("new-greetings"),
{ok, [{greetings, Greetings}, {timestamp, Timestamp}]}.
-module(greeting, [Id, GreetingText]).
-compile(export_all).
validation_tests() ->
[
{fun() -> length(GreetingText) > 0 end, "Greeting must be non-empty!"},
{fun() -> length(GreetingText) =< 140 end, "Greeting must be tweetable"}
].
before_create() ->
ModifiedRecord = set(greeting_text, re:replace(GreetingText, "masticate", "chew", [{return, list}])),
{ok, ModifiedRecord}.
after_create() ->
boss_mq:push("new-greetings", THIS).
<b>{{ greeting|upper }}</b>
<html>
<head>
<title>{% block title %}Greetings!{% endblock %}</title>
</head>
<body>
{% block body %}
<ul>
{% if greetings %}
{% for greeting in greetings %}
<li>{{ greeting.greeting_text }}
{% endfor %}
{% else %}
<li>No greetings!
{% endif %}
</ul>
<p><a href="{% url action="create" %}">New greeting...</a></p>
<form method="post" action="{% url action="goodbye" %}">
Delete:
<select name="greeting_id">
{% for greeting in greetings %}
<option value="{{ greeting.id }}">{{ greeting.greeting_text }}
{% endfor %}
</select>
<input type="submit">
</form>
{% endblock %}
</body>
</html>
{% extends "greeting/list.html" %}
{% block title %}A new greeting!{% endblock %}
{% block body %}
{% if errors %}
<ul>
{% for error in errors %}
<li><font color=red>{{ error }}</font>
{% endfor %}
</ul>
{% endif %}
<form method="post">
Enter a new greeting:
<textarea name="greeting_text">{% if new_msg %} {{ new_msg.greeting_text }}{% endif %}</textarea> <input type="submit">
</form>
{% endblock %}
<html>
<head>
<title>Fresh hot greetings!</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script>
function listen_for_events(timestamp) {
$.ajax("/greeting/pull/"+timestamp, {
success: function(data, code, xhr) {
for (var i=0; i<data.greetings.length; i++) {
var msg = data.greetings[i].greeting_text;
$("#greeting_list").append("<li>"+msg);
}
listen_for_events(data.timestamp);
}
});
}
$(document).ready(function() {
listen_for_events({{ timestamp }});
});
</script>
</head>
<body>
<ul id="greeting_list">
{% for greeting in greetings %}
<li>{{ greeting.greeting_text }}
{% empty %}
<li>No greetings!
{% endfor %}
</ul>
</body>
</html>