Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PlayerController
`import Ember from 'ember'`
PlayerController = Ember.Controller.extend( Ember.Evented,
responses: Ember.A()
stepsAndResponses: Ember.A()
currentStep: null
updateResponseBlock: ->
@responses.clear()
responsesIds = @currentStep.get('responses_ids')
filter = @store.filter('response', (response) ->
responseId = response.get('id')
responsesIds.contains(responseId)
)
filter.then (responses) =>
responsesArray = responses.toArray()
@responses.pushObjects(responsesArray)
onViewInsert: ->
@itemDataParser.parse(@get('model').data)
filter = @store.filter('step', (step) -> step.get 'root')
filter.then (steps) =>
step = steps.objectAt(0)
@addStep step
@currentStep = step
@updateResponseBlock()
@trigger 'addedPlayerItem'
nextStepFromArrayOfSteps: (nextStepId, allSteps) ->
filtered = allSteps.filter (step) -> step.id == nextStepId.toString()
filtered[0]
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'))
mergedStep
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
else
@currentStep = step
@stepsAndResponses.pushObject step
finishItem: ->
@cleanup()
@transitionToRoute('map')
cleanup: ->
@stepsAndResponses.clear()
@responses.clear()
@store.unloadAll('step')
@store.unloadAll('response')
@store.unloadAll('item')
actions:
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
@updateResponseBlock()
else
@finishItem()
@trigger 'addedPlayerItem'
)
`export default PlayerController`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment