Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Client-side stuff, state saving

  • Loading branch information...
commit 7dd689d6b954f3e82b59e4af0a57a8ca7f6ece5a 1 parent 1a3f621
@1602 1602 authored
View
3  app/controllers/messages_controller.js
@@ -4,7 +4,7 @@ before(loadMessage, {only: ['show', 'edit', 'update', 'destroy']});
action('new', function () {
this.title = 'New message';
- this.message = new Message;
+ this.message = new Message({'status': 'NEW'});
render();
});
@@ -19,6 +19,7 @@ action(function create() {
} else {
message.send(function (status) {
send(status);
+ message.updateAttribute('status', status.state, console.log);
});
}
});
View
14 app/views/messages/_form.ejs
@@ -23,3 +23,17 @@
<%- form.input("message") %>
</div>
</div>
+
+<div class="control-group">
+ <%- form.label("type", 'Message type', {class: "control-label"}) %>
+ <div class="controls">
+ <label class="radio">
+ <input type="radio" name="Message[type]" value="voice" <%= message.type === 'voice' ? ' checked': '' %> />
+ voice
+ </label>
+ <label class="radio">
+ <input type="radio" name="Message[type]" value="sms" <%= message.type === 'sms' ? ' checked': '' %> />
+ SMS
+ </label>
+ </div>
+</div>
View
21 app/views/messages/new.ejs
@@ -2,10 +2,29 @@
<h1>New message</h1>
</div>
-<% form_for(message, {action: path_to.messages(), method: 'POST', id: "message_form", class: 'form-horizontal'}, function (form) { %>
+<% form_for(message, {action: path_to.messages(), 'data-jsonp': 'handleResult', 'data-remote': true, method: 'POST', id: "message_form", class: 'form-horizontal'}, function (form) { %>
<%- partial('messages/form.ejs', {locals: {form: form, message: message}}) %>
+ <div class="result" style="font-size: 30px;">
+ </div>
<div class="form-actions">
<%- form.submit('<i class="icon-ok icon-white"></i> Create message', {class: 'btn btn-primary'}) %> or
<%- link_to('Cancel', path_to.messages(), {class: 'btn'}) %>
</div>
<% });%>
+
+<script>
+ $(function () {
+ $('.form-actions button').click(function () {
+ var $btn = $(this);
+ setTimeout(function () {
+ $btn.attr('disabled', true).addClass('disabled');
+ }, 100);
+ });
+ });
+ function handleResult(res) {
+ $('.form-actions button').attr('disabled', false).removeClass('disabled');
+ $('.result').html('STATE: ' + res.state);
+ // console.log(res);
+ }
+</script>
+
View
4 app/views/messages/show.ejs
@@ -9,6 +9,8 @@
<tr><th>lastname</th><td><%= message.lastname %></td></tr>
<tr><th>phone</th><td><%= message.phone %></td></tr>
<tr><th>message</th><td><%= message.message %></td></tr>
+ <tr><th>type</th><td><%= message.type %></td></tr>
+ <tr><th>status</th><td><%= message.status %></td></tr>
</tbody>
</table>
@@ -17,4 +19,4 @@
<%- link_to('<i class="icon-remove icon-white"></i> Delete', path_to.message(message), {class: 'btn btn-danger', method: 'delete', remote: true, jsonp: '(function (u) { location.href = u; })'}) %>
or
<%- link_to('Back to index', path_to.messages()) %>
-</div>
+</div>
View
5 db/schema.js
@@ -26,4 +26,7 @@ var Message = describe('Message', function () {
property('lastname', String);
property('phone', String);
property('message', String);
-});
+ property('type', String, {default: 'voice'}); // sms, voice
+ property('status');
+});
+
View
28 lib/tropo.js
@@ -2,7 +2,6 @@ var tropoSessions = {};
var tropoSessionsIndex = {};
var https = require('https');
var API = require('tropo-webapi');
-var tropo = new TropoWebAPI();
var token = '124cdb4120bef148a47b0e84a7eabe335c19828a326fcb47c6aa4c0565948b2692d5570d64f7a8f5dd047e90';
app.startTropoSession = function (message, callback) {
@@ -30,14 +29,18 @@ app.post('/tropo/text', function (req, res) {
console.log('send tropo task');
+ var tropo = new TropoWebAPI();
var messageId = req.body.session.parameters.messageId;
var sessionId = req.body.session.id;
var session = tropoSessions[messageId];
var message = session.message;
tropoSessionsIndex[sessionId] = messageId;
- // tropo.call(to, null, null, null, null, null, "SMS", null, null, null);
- tropo.call(message.phone);
+ if (message.type === 'sms') {
+ tropo.call(message.phone, null, null, null, null, null, "SMS", null, null, null);
+ } else {
+ tropo.call(message.phone);
+ }
tropo.say(message.message);
callbacks.forEach(function (callback) {
tropo.on(callback, null, '/tropo/callback/' + callback, true);
@@ -48,7 +51,7 @@ app.post('/tropo/text', function (req, res) {
});
callbacks.forEach(function (callback) {
- app.post('/tropo/callback/' + callback, callbackHandler(callback));
+ app.all('/tropo/callback/' + callback, callbackHandler(callback));
});
function callbackHandler(callback) {
@@ -57,25 +60,10 @@ function callbackHandler(callback) {
var messageId = tropoSessionsIndex[sessionId];
var session = tropoSessions[messageId];
req.body.result.callback = callback;
- session.callback.send(req.body.result);
+ session.callback(req.body.result);
// delete tropoSessions[messageId];
// delete tropoSessionsIndex[sessionId];
res.send(200);
};
}
-app.post('/messages', function (req, res) {
-
- app.models.Message.create(req.body.Message, function (err, message) {
- if (err) {
- flash('error', 'Message can not be created');
- render('new', {
- message: message,
- title: 'New message'
- });
- } else {
- message.send(res);
- }
- });
-});
-
Please sign in to comment.
Something went wrong with that request. Please try again.