Skip to content

Instantly share code, notes, and snippets.

@jeffreytgilbert
Last active December 18, 2015 14:48
Show Gist options
  • Save jeffreytgilbert/5799307 to your computer and use it in GitHub Desktop.
Save jeffreytgilbert/5799307 to your computer and use it in GitHub Desktop.
'use strict'
console.log "I'm the client code"
# A global reference, because coffeescript wraps everything in a closure
root = global ? window
root.Meteor.subscribe 'messages'
root.Meteor.subscribe 'rooms'
# root.Meteor.startup ->
# Deps.autorun ->
#
# Messages logic goes here:
#
# query initial messages for the room (doesnt work without a room id. needs a fix now)
root.Template.messages.messages = ->
return root.Messages.find {}, sort: time:-1
# bind event handlers to room inputs (needs to be fixed to handle multiple rooms)
root.Template.entry.events = 'keydown input': (event) ->
if event.which is 13
event.preventDefault
event.stopPropogation
userId = root.Meteor.userId()
if userId ? null
# if doc.room_id and userId and doc.message isnt '' and doc.name isnt '' and doc.sender_id
message = {
room_id: 1,
sender_id: userId,
name:root.Meteor.user().emails[0].address,
message: event.target.value,
time: Date.now()/1000
}
console.log message
root.Messages.insert message
event.target.value = ''
else
alert 'You need to be logged in to post!'
return false
#
# Rooms logic goes here
#
# query initial room list
root.Template.rooms.availableRooms = ->
return root.Rooms.find {}
# have event handlers for the things that trigger new rooms to be created and or opened
root.Template.rooms.events = 'click .new_room_link': (event) ->
userId = root.Meteor.userId()
if userId ? null
room = {
owner: userId,
name: root.Meteor.user().emails[0].address,
thumbnail: 'http://url',
isOnline: true,
public: true
}
debugger
# console.log room
root.Rooms.insert room
# I shouldn't need to set this again, right?
# root.Template.rooms.availableRooms = root.Rooms.find {}
return true
else
alert 'You must have logged in to create a new room.'
return false
(function(){ 'use strict';
var root;
console.log("I'm the client code");
root = typeof global !== "undefined" && global !== null ? global : window;
root.Meteor.subscribe('messages');
root.Meteor.subscribe('rooms');
root.Template.messages.messages = function() {
return root.Messages.find({}, {
sort: {
time: -1
}
});
};
root.Template.entry.events = {
'keydown input': function(event) {
var message, userId;
if (event.which === 13) {
event.preventDefault;
event.stopPropogation;
userId = root.Meteor.userId();
if (userId != null ? userId : null) {
message = {
room_id: 1,
sender_id: userId,
name: root.Meteor.user().emails[0].address,
message: event.target.value,
time: Date.now() / 1000
};
console.log(message);
root.Messages.insert(message);
event.target.value = '';
} else {
alert('You need to be logged in to post!');
}
return false;
}
}
};
root.Template.rooms.availableRooms = function() {
return root.Rooms.find({});
};
root.Template.rooms.events = {
'click .new_room_link': function(event) {
var room, userId;
userId = root.Meteor.userId();
if (userId != null ? userId : null) {
room = {
owner: userId,
name: root.Meteor.user().emails[0].address,
thumbnail: 'http://url',
isOnline: true,
"public": true
};
debugger;
root.Rooms.insert(room);
root.Template.rooms.availableRooms = root.Rooms.find({});
return true;
} else {
alert('You must have logged in to create a new room.');
return false;
}
}
};
}).call(this);
<head>
<title>Chat</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header id="main_header">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Project name</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="javascript://">About</a></li>
<li><a href="javascript://">Contact</a></li>
</ul>
<div class="navbar-form pull-right">
<ul class="nav">
<li>{{loginButtons align="right"}}</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</header>
<div class="container">
{{> rooms}}
</div>
</body>
<template name="rooms">
{{#each availableRooms}}
<div class="span4">
{{> entry}}
{{> messages}}
</div>
{{else}}
<div>
Uh oh! Looks like there are no active rooms currently. Maybe you'd like to host a chat?
<a class="new_room_link" href="javascript://">Start a room of your own!</a>
</div>
{{/each}}
</template>
<template name="open_rooms">
{{#each availableUsers}}
<div>
<img src="{{thumbnail}}">
<strong>{{name}}</strong>
</div>
{{/each}}
</template>
<template name="entry">
<form class="form-inline">
<p>
<input type="text" name="message_box" class="input-block-level" placeholder="Your message box">
</p>
</form>
</template>
<template name="messages">
{{#each messages}}
{{> message}}
{{/each}}
</template>
<template name="message">
<p><strong>{{name}}</strong> {{message}} </p>
</template>
'use strict'
console.log "I'm the models, you know what i mean"
# Because coffeescript makes everything wrapped nicely in a closure,
# reference the global space to store the models so they can be accessed
# by server and client scripts alike
root = global ? window
# if ever you need to clear something in bulk, do it here
# root.Messages.remove({});
root.Messages = new Meteor.Collection "messages"
root.Rooms = new Meteor.Collection "rooms"
root.Messages.allow {
insert: (userId, doc) ->
if doc.room_id and userId and doc.message isnt '' and doc.name isnt '' and doc.sender_id
return true
else
return false
update: (userId, doc, fieldNames, modifier) ->
return false
remove: (userId, doc) ->
# if user_id is 1
# return true
# else
return false
}
root.Rooms.allow {
insert: (userId, doc) ->
if userId
return true
update: (userId, doc, fieldNames, modifier) ->
if doc.owner is userId
return true
remove: (userId, doc) ->
if userId isnt null and doc.is_admin is true
return true
}
# other types of allow limitation are fetch and transform.
# fetch limits the fields fetchable by update or remove. not sure about transform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment