EmberJS advice on controller, service and component usage
<p>I'm trying to figure out how to properly handle the communication between a controller and several components. I think I need to use a service here, but maybe another solution would me more appropriate.</p>

<p>The idea is that users get served a lesson where one question is given. When a user clicks on the check button, a request to the backend API should be made to check whether an answer is correct. The tricky part is that questions can have different types: open questions, multiple choice and drag and drop.</p>

<p>There is a /lesson/template.hbs which summarized looks like this:</p>

<pre><code>// lesson/template.hbs
&lt;h2&gt;{{model.question.lesson.title}}&lt;/h2&gt;

&lt;div class="callout"&gt; {{{model.question.body}}} &lt;/div&gt;

{{#if model.question.isMultipleChoice}}
    {{answer-multiple-choice-list answers=model.question.answers}}
{{else if model.question.isDragDrop}}
    {{answer-drag-drop question=model.question}}
{{else if model.question.isOpen}}
    {{answer-open-textarea question=model.question checkAnswer="checkAnswer"}}
{{/if}}

{{#if question_is_answered}}
&lt;a class="next button" {{action 'nextQuestion'}}&gt;Next&lt;/a&gt;
{{else}}
&lt;a class="check button" {{action 'checkAnswer'}}&gt;Check&lt;/a&gt;
{{/if}}
</code></pre>

<p>Then, my idea was to have a lesson/controller.js which handles the both the nextQuestion and checkAnswer click actions:</p>

<pre><code>// lesson/controller.js
export default Ember.Controller.extend({

    lesson: Ember.inject.service(),

    errorObserver: Ember.on('init', Ember.observer('lesson.has_error', function() {
        console.log('Error detected');
    })),

    actions: {
        checkAnswer() {
            this.set('lesson.is_check_pressed', true);
        }
    }

});
</code></pre>

<p>And to have a service who is responsible for glueing the controller and corresponding component:</p>

<pre><code>// lesson/service.js
export default Ember.Service.extend({

    is_check_pressed: false,
    has_error: false,

    checkObserver: Ember.observer('is_check_pressed', function() {
        console.log('LessonService: Observer: is_check_pressed changed to', this.get('is_check_pressed'));
    }),

    errorObserver: Ember.on('init', Ember.observer('has_error', function() {
        console.log('LessonService: Observer: has_error changed');
    })),

});
</code></pre>

<p>This service, in the end, will also contain an ajax request to the backend api.</p>

<p>And, for example, the answer-open-textarea component looks as follows:</p>

<pre><code>export default Ember.Component.extend({

    lesson: Ember.inject.service(),
    given_answer: '',

    checkObserver: Ember.on('init', Ember.observer('lesson.is_check_pressed', function() {
        console.log('OpenTextAreaComponent, checkObserver');

        if( Ember.isBlank( this.get('given_answer') ) )
        {
            this.set('lesson.is_check_pressed', false);
            this.set('lesson.has_error', true);
        }
        else
        {
            // Update variable @ service so that the controller knows that the nextQuestion button can be active
        }
    })),

});
</code></pre>

<p>The logic for validating the input, should be in the component, since it does not make sense to have the validity checks for all the possible question types in the controller.</p>

<p>Unfortunately, the code above does not work the way it should, even though this is just the basic idea. What works is, when the check button is pressed, the component checkObserver is fired, as it logs to the console. But it does not trigger the errorObserver in the controller, when updating lesson.has_error in the service. And, even if this would work, both the controller and the component will have several obeservers which I don't feel like to be a good solution.</p>

<p>My question is: how can controllers and components "communicate" in a proper way? By using a service? If yes, then how would that look like? I guess not via Observers.</p>

<p>Thanks in advance!</p>