Last active
December 18, 2015 14:48
-
-
Save jeffreytgilbert/5799307 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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