Skip to content

Commit eb69a04

Browse files
committed
Slides and beginning of the application.
1 parent d2bb954 commit eb69a04

File tree

5 files changed

+122
-8
lines changed

5 files changed

+122
-8
lines changed

workshop7/Intro to jQuery - 7.pdf

62.6 KB
Binary file not shown.

workshop7/server/app/views/index.ejs

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,123 @@
2525
</div>
2626
</div>
2727
</nav>
28-
<div class="container">
28+
<div class="starter-template">
29+
<div class="container">
30+
<div class="form-horizontal">
31+
<div class="form-group">
32+
<div class="col-sm-8">
33+
<input type="text" id="username" class="form-control" placeholder="Enter a username" />
34+
</div>
35+
<div class="col-sm-4">
36+
<button class="btn btn-info" onclick="enterUsername()">Enter</button>
37+
</div>
38+
</div>
39+
</div>
40+
<div id="chatArea" style="display:none;">
41+
<div class="form-horizontal">
42+
<div class="form-group">
43+
<div class="col-sm-8">
44+
<input type="text" id="roomName" class="form-control" placeholder="Create a room" />
45+
</div>
46+
<div class="col-sm-4">
47+
<button class="btn btn-info" onclick="createRoom()">Create</button>
48+
</div>
49+
</div>
50+
</div>
51+
52+
<div class="row">
53+
<div class="col-sm-8">
54+
<div class="form-horizontal">
55+
<h4>Add a Post</h4>
56+
<div class="form-group">
57+
<div class="col-sm-8">
58+
<input type="text" id="message" class="form-control" placeholder="Say what?" />
59+
</div>
60+
<div class="col-sm-4">
61+
<button class="btn btn-info" onclick="postMessage()">Post</button>
62+
</div>
63+
</div>
64+
</div>
65+
66+
<h4>Posts</h4>
67+
<div id="posts">
2968

69+
</div>
70+
</div>
71+
72+
<div class="col-sm-4">
73+
<h4>Rooms</h4>
74+
<div id="rooms">
75+
76+
</div>
77+
</div>
78+
</div>
79+
</div>
80+
</div>
3081
</div>
82+
<script type="text/javascript">
83+
var baseUrl = 'http://mainstreetchat-kylepace.rhcloud.com/';
84+
var username, currentRoom;
85+
var loadChatRooms = function () {
86+
var roomArea = $('#rooms');
87+
$.getJSON(baseUrl + '/chatroom', function (chatRooms) {
88+
chatRooms.forEach(function (room) {
89+
roomArea.append("<p><a href=\"javascript:void(0);\" onclick=\"goToRoom('" + room._id + "')\">" + room.name + "</a><p/>");
90+
});
91+
});
92+
};
93+
94+
var postMessage = function () {
95+
var message = $('#message');
96+
if (message.val()) {
97+
$.post(baseUrl + '/chatroom/' + currentRoom._id + '/post', { username: username, text: message.val() }, function () {
98+
message.val('');
99+
goToRoom(currentRoom._id);
100+
});
101+
} else {
102+
alert('You need to enter text for a message.');
103+
}
104+
};
105+
106+
var goToRoom = function (id) {
107+
var postsArea = $('#posts');
108+
postsArea.empty();
109+
$.getJSON(baseUrl + '/chatroom/' + id, function (room) {
110+
currentRoom = room;
111+
room.posts.forEach(function (post) {
112+
postsArea.append(
113+
'<p>By: ' + post.username + '</p>' +
114+
'<p>' + post.text + '</p>'
115+
);
116+
});
117+
});
118+
};
119+
120+
var showChatArea = function () {
121+
loadChatRooms();
122+
$('#chatArea').show();
123+
};
124+
125+
var enterUsername = function () {
126+
var usernameVal = $('#username').val();
127+
if (!usernameVal) {
128+
alert('You must enter a username');
129+
} else {
130+
username = usernameVal;
131+
showChatArea();
132+
}
133+
};
134+
135+
var createRoom = function () {
136+
var roomName = $('#roomName').val();
137+
if (!roomName) {
138+
alert('You must enter a room name');
139+
} else {
140+
$.post(baseUrl + '/chatroom', { name: roomName }, function () {
141+
alert('done');
142+
});
143+
}
144+
};
145+
</script>
31146
</body>
32147
</html>

workshop7/server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "ISC",
1111
"dependencies": {
1212
"body-parser": "^1.12.4",
13+
"cors": "^2.6.0",
1314
"ejs-locals": "^1.0.2",
1415
"express": "^4.12.4",
1516
"express-cors": "0.0.3",

workshop7/server/server.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ var newrelic = require('newrelic'),
22
express = require('express'),
33
app = express(),
44
engine = require('ejs-locals'),
5-
cors = require('express-cors'),
5+
cors = require('cors'),
66
bodyParser = require('body-parser'),
77
mongoose = require('mongoose'),
88
_ = require('lodash'),
9-
ChatRoom = require('./app/models/chatroom');
9+
ChatRoom = require('./app/models/chatrooms');
10+
11+
app.use(cors());
1012

1113
app.engine('ejs', engine);
1214
app.use('/public', express.static(__dirname + '/public'));
@@ -16,10 +18,6 @@ app.set('views', __dirname + '/app/views');
1618
app.use(bodyParser.urlencoded({ extended: true }));
1719
app.use(bodyParser.json());
1820

19-
app.use(cors({
20-
allowedOrigins: ['*']
21-
}));
22-
2321
app.get('/', function (req, res) {
2422
res.render('index');
2523
});
@@ -121,7 +119,7 @@ app.post('/chatroom/:id/post', function (req, res) {
121119

122120
var connectionString = 'localhost/mainstreet-chatroom';
123121
if (process.env.OPENSHIFT_MONGODB_DB_URL) {
124-
connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + 'mainstreet-chatroom';
122+
connectionString = process.env.OPENSHIFT_MONGODB_DB_URL + 'mainstreetchat';
125123
}
126124
mongoose.connect(connectionString);
127125

0 commit comments

Comments
 (0)