Created
July 3, 2013 16:00
-
-
Save spikebrehm/5919771 to your computer and use it in GitHub Desktop.
An example base model for Rendr, showing a way to use relations between models.
This file contains hidden or 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
_ = 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() |
This file contains hidden or 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
BaseModel = require('./base') | |
module.exports = class Reservation extends BaseModel | |
@id = 'Reservation' | |
jsonKey: 'reservation' | |
relations: | |
listing: {model: 'Listing'} | |
guest: {model: 'User'} | |
host: {model: 'User'} |
This file contains hidden or 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
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