Skip to content

Instantly share code, notes, and snippets.

@dustinfarris
Last active December 19, 2015 20:49
Show Gist options
  • Save dustinfarris/6015704 to your computer and use it in GitHub Desktop.
Save dustinfarris/6015704 to your computer and use it in GitHub Desktop.
Pre-populate children and grandchildren when a parent is created. The app has a set of bridge—the card game—Exercises, each Exercise consisting of 8 Hands, each Hand having an Answer for each Row within the Exercise's associated Situation.
App.ExercisesNewController = App.ObjectController.extend
create: ->
situation_id = $('#id_situation').val()
situation = App.Situation.find(situation_id)
@content.set 'situation', situation
rows = situation.get('rows')
@content.save().then =>
for i in [1..8]
hand = App.Hand.createRecord
exercise: @content
order: i
hand.save().then (hand) =>
rows.forEach (row) ->
answer = App.Answer.createRecord
row: row
hand: hand
min: ''
mid: ''
max: ''
answer.save()
@store.commit()
@success "Exercise created!"
@transitionToRoute 'exercises.edit', @content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment