Skip to content

Commit

Permalink
Put needed structure into the MQ instead of using Model
Browse files Browse the repository at this point in the history
  • Loading branch information
djo committed Mar 27, 2013
1 parent bf5679b commit 0283f1e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
6 changes: 1 addition & 5 deletions priv/init/kaede_01_news.erl
Expand Up @@ -6,11 +6,7 @@
% return a list of WatchIDs that should be cancelled in the stop
% function below (stop is executed if the script is ever reloaded).
init() ->
{ok, WatchTopics} = boss_news:watch("topics",
fun(created, Topic) ->
boss_mq:push("new-topics", {topic, Topic})
end),
{ok, [WatchTopics]}.
{ok, []}.

stop(ListOfWatchIDs) ->
lists:map(fun boss_news:cancel_watch/1, ListOfWatchIDs).
Expand Down
25 changes: 16 additions & 9 deletions priv/static/js/app.js
@@ -1,4 +1,4 @@
function Topics (startTimestamp) {
function Topics () {
var topicTemplate = _.template($("#topic").html()),
topicList = $(".topics"),
topicForm = $("form.new_topic"),
Expand All @@ -7,19 +7,18 @@ function Topics (startTimestamp) {
self = {
addTopics: function (topics) {
_.each(topics, function (topic) {
topicList.append(topicTemplate(topic))
topicList.prepend(topicTemplate(topic))
})
},

createTopic: function (e) {
e.preventDefault()

$.ajax({
url: "/topic/create",
url: e.target.action,
type: 'POST',
data: { topic_text: topicText.val() }
}).success(function (data) {
console.log(data)
topicText.val("")
}).error(function (jqXHR) {
alert("Errors: " + jqXHR.responseText)
Expand All @@ -28,17 +27,25 @@ function Topics (startTimestamp) {

poll: function (timestamp) {
setTimeout(function () {
$.get("/topic/pull/" + timestamp, function (data) {
console.log(data)
self.addTopics(data.topics)
self.poll(data.timestamp)
$.get("/topic/poll/" + timestamp)
.success(function (data) {
self.addTopics(data.topics)
self.poll(data.timestamp)
})
}, 1000)
},

fetchStartTopics: function () {
$.get("/topic")
.success(function (data) {
self.addTopics(data.topics)
self.poll(data.timestamp)
})
},

init: function () {
topicForm.submit(self.createTopic)
self.poll(startTimestamp)
self.fetchStartTopics()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/controller/kaede_dashboard_controller.erl
Expand Up @@ -8,5 +8,4 @@ before_(_, _, _) ->
end.

index('GET', [], Member) ->
Timestamp = boss_mq:now("new-topics"),
{ok, [{member, Member}, {timestamp, Timestamp}]}.
{ok, [{member, Member}]}.
12 changes: 10 additions & 2 deletions src/controller/kaede_topic_controller.erl
@@ -1,13 +1,19 @@
-module(kaede_topic_controller, [Req]).
-export([before_/3, pull/3, create/3]).
-export([before_/3, index/3, poll/3, create/3]).

before_(_, _, _) ->
case member_lib:require_login(Req) of
fail -> {redirect, "/member/login"};
{ok, Member} -> {ok, Member}
end.

pull('GET', [Timestamp], Member) ->
index('GET', [], Member) ->
Topics = boss_db:find(topic, []),
MQTopics = lists:map(fun(Topic) -> topic_mq:build(Topic, Member) end, Topics),
Timestamp = boss_mq:now("new-topics"),
{json, [{topics, MQTopics}, {timestamp, Timestamp}]}.

poll('GET', [Timestamp], Member) ->
{ok, NewTimestamp, Topics} = boss_mq:pull("new-topics",
list_to_integer(Timestamp)),
{json, [{timestamp, NewTimestamp}, {topics, Topics}]}.
Expand All @@ -17,6 +23,8 @@ create('POST', [], Member) ->
Topic = topic:new(id, TopicText),
case Topic:save() of
{ok, Saved} ->
MQTopic = topic_mq:build(Saved, Member),
boss_mq:push("new-topics", MQTopic),
{json, [{topic, Saved}]};
{error, Errors} ->
{json, [{errors, Errors}]}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/topic_mq.erl
@@ -0,0 +1,9 @@
-module(topic_mq).
-export([build/2]).

% Builds the Topic structure which will be used in the message queue
build(Topic, Member) ->
erlang:display(Member),
[{topic_id, Topic:id()},
{topic_text, Topic:topic_text()},
{member_name, Member:name()}].
14 changes: 10 additions & 4 deletions src/view/dashboard/index.html
Expand Up @@ -12,23 +12,29 @@ <h2>Topics</h2>
</div>
</div>

<form method="post" class="form-inline new_topic">
<form method="post" action="/topic/create" class="form-inline new_topic">
<input type="text" placeholder="New topic" name="topic_text" class="topic_text">
<button type="submit" class="btn">Add</button>
</form>

<ul class="topics"></ul>
<div class="topics"></div>

<script type="text/html" id='topic'>
<li><%= topic_text %></li>
<div class="topic" data-topic="<%= topic_id %>">
<h4>
<a href="#messages"><%= topic_text %></a>
<small><%= member_name %></small>
</h4>
<div class="messages"></div>
</div>
</script>
{% endblock %}

{% block scripts %}
<script src="/static/js/app.js"></script>
<script>
$(function () {
new Topics('{{timestamp}}')
new Topics()
})
</script>
{% endblock %}

0 comments on commit 0283f1e

Please sign in to comment.