Created
January 9, 2012 21:31
-
-
Save petebacondarwin/1585046 to your computer and use it in GitHub Desktop.
Spine Nested Models
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
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