Created
May 2, 2014 16:45
-
-
Save mrmurphy/0f44d4e666508c326f5e to your computer and use it in GitHub Desktop.
Message
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
db = require "../lib/db" | |
Q = require "q" | |
_ = require "lodash" | |
GROUP = "messages" | |
class Message | |
constructor: (obj) -> | |
# Assign all properties of the object to this. | |
_.extend(this, obj) | |
if obj.id is undefined | |
@id = null | |
save: => | |
Q(do => | |
if @id is null | |
return db.getNewIdFor(GROUP).then (id) => | |
@id = id | |
return this | |
else | |
return this | |
).then => | |
db.save GROUP, @id, this | |
.then => | |
return this | |
.catch (err) -> | |
console.log "There was an error: #{err}" | |
# This does't operate on an instance, it just returns | |
# a message object from the database. | |
get: (id) -> | |
db.get GROUP, id | |
.then (obj) -> | |
msg = new Message(obj) | |
module.exports = Message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment