Skip to content

Instantly share code, notes, and snippets.

@por
Forked from spikebrehm/base.coffee
Created October 31, 2013 17:52
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 por/7254016 to your computer and use it in GitHub Desktop.
Save por/7254016 to your computer and use it in GitHub Desktop.
_ = require('underscore')
RendrBase = require('rendr/shared/base/model')
module.exports = class Base extends RendrBase
constructor: ->
super
@initRelations()
initRelations: ->
return unless @relations?
keys = Object.keys(@relations)
@updateRelation(key) for key in keys
keys.forEach (key) =>
@on "change:#{key}", => @updateRelation(key)
updateRelation: (key) ->
relation = @relations[key]
modelOrCollection = Object.keys(relation)[0]
path = _.values(relation)[0]
Constructor = modelUtils.getConstructor(modelOrCollection, path)
if @has(key)
property = relation.alias || key
obj = @[property] = new Constructor(@get(key), parse: true, app: @app)
obj.store()
BaseModel = require('./base')
module.exports = class Reservation extends BaseModel
@id = 'Reservation'
jsonKey: 'reservation'
relations:
listing: {model: 'Listing'}
guest: {model: 'User'}
host: {model: 'User'}
BaseModel = require('./base')
module.exports = class Thread extends BaseModel
@id = 'Thread'
jsonKey: 'thread'
relations:
posts:
collection: 'Messages'
alias: 'messages'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment