Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
`import Ember from 'ember'`
PlayerController = Ember.Controller.extend( Ember.Evented,
responses: Ember.A()
stepsAndResponses: Ember.A()
currentStep: null
updateResponseBlock: ->
responsesIds = @currentStep.get('responses_ids')
filter = @store.filter('response', (response) ->
responseId = response.get('id')
filter.then (responses) =>
responsesArray = responses.toArray()
onViewInsert: ->
filter = @store.filter('step', (step) -> step.get 'root')
filter.then (steps) =>
step = steps.objectAt(0)
@addStep step
@currentStep = step
@trigger 'addedPlayerItem'
nextStepFromArrayOfSteps: (nextStepId, allSteps) ->
filtered = allSteps.filter (step) -> == nextStepId.toString()
fillMergedStep: (mergedStep) ->
allSteps = @store.all 'step'
nextStepId = mergedStep.get 'next_step_id'
currentlyMergingStep = null
mergeToNext = true
prompt = mergedStep.get 'prompt'
while mergeToNext
currentlyMergingStep = @nextStepFromArrayOfSteps(nextStepId, allSteps)
prompt += " #{currentlyMergingStep.get 'prompt'}"
mergeToNext = currentlyMergingStep.get 'merge_to_next'
nextStepId = currentlyMergingStep.get 'next_step_id'
mergedStep.set('prompt', prompt)
mergedStep.set('responses_ids', currentlyMergingStep.get('responses_ids'))
addStep: (step) ->
if step.get 'merge_to_next'
mergedStep = @store.createRecord('step'
fake: true
responsesIds: step.get 'responses_ids'
nextStepId: step.get 'next_step_id'
prompt: step.get 'prompt'
mergedStep = @fillMergedStep(mergedStep)
@currentStep = mergedStep
@stepsAndResponses.pushObject mergedStep
@currentStep = step
@stepsAndResponses.pushObject step
finishItem: ->
cleanup: ->
responseChosen: (response) ->
nextStepId = response.get('next_step_id')
if nextStepId
@stepsAndResponses.addObject {playerResponse: true, prompt: response.get 'value'}
nextStep = @store.getById('step', nextStepId)
@addStep nextStep
@trigger 'addedPlayerItem'
`export default PlayerController`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment