Skip to content

Instantly share code, notes, and snippets.

@petebacondarwin
Created January 9, 2012 21:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petebacondarwin/1585046 to your computer and use it in GitHub Desktop.
Save petebacondarwin/1585046 to your computer and use it in GitHub Desktop.
Spine Nested Models
Spine = require('spine')
require('spine/lib/relation')
class Survey extends Spine.Model
@configure 'Survey', 'name', 'title', 'description'
@hasMany 'questions', Question
@fromJSON: (objects) ->
return unless objects
if typeof objects is 'string'
objects = JSON.parse(objects)
survey = new Survey(objects)
constructor: (objects)->
super
survey.questions().create(q) for q in objects.questions
class Question extends Spine.Model
@configure 'Question', 'name', 'title', 'description'
@belongsTo 'survey', Survey
@hasMany 'choices', Choice
constructor: (objects)->
super
survey.choices().create(c) for c in objects.choices
class Choice extends Spine.Model
@configure 'Choice', 'name', 'title', 'value'
@belongsTo 'question', Question
module.exports.Survey = Survey
module.exports.Question = Question
module.exports.Choice = Choice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment