Skip to content

Instantly share code, notes, and snippets.

@dbackeus
Created October 13, 2014 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbackeus/c09ae1ff6c42a0e004fa to your computer and use it in GitHub Desktop.
Save dbackeus/c09ae1ff6c42a0e004fa to your computer and use it in GitHub Desktop.
Meteor Model concept
root = global ? window
Function::property = (prop, descriptor) ->
Object.defineProperty this.prototype, prop, descriptor
Function::include = (module) ->
for name, value of module
@prototype[name] = value
Function::extend = (module) ->
for name, value of module
@[name] = value
class Model
@ClassMethods:
find: (args...) ->
@collection.find.apply(@collection, args)
findOne: (args...) ->
@collection.findOne.apply(@collection, args)
insert: (args...) ->
@collection.insert.apply(@collection, args)
@property "id", get: -> @._id
constructor: (attributes) ->
@attributes = attributes
for name, value of attributes
@property = name
root.Model = Model
class Story extends Model
@extend Model.ClassMethods
@collection = new Meteor.Collection "stories", transform: (doc) => new @(doc)
root.Story = Story
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment