3
votes

i tried out a simple application, but i am confused as when to use model hook in controller and when to go for model hook in route. Take this for example(using EmberCLI):

Template (templates/discovery.hbs)

{{#each model}}
    <tr>
        <td>
            Q: {{ques}}
        </td>
    </tr>
    {{/each}}

so i can define model in following two ways

**First Way (routes/discovery.js) **

import Ember from "ember";

export default Ember.Route.extend({
    model : function(){
        return this.store.all('questions') ; 
    }
});

This works just as expected, all the record of type questions are passed on and displayed in discovery.hbs template.

Second way (controllers/discovery.js)

import Ember from "ember";

export default Ember.ArrayController.extend({
    model : function(){
        return this.store.all('questions') ; 

    }
});

So this was expected to work in same way like the previous(atleast i expected it to), but this doesn't display any record. So what is difference if i define model in route or controller? What should be preferred?

1
model hook is only for the route. What ever you return from that method becomes the value for the 'model' property of the controller. AFAIK model is property on the controller and not a hook you can override.blessenm
Ahm , so i was guessing ember should throw some error if try to do that? , Like : model property on controller cannot be overidden ?zapper
The definition of model in the code is model: computed.alias('content'). And it should stay overridable because that is what you sometimes do in setupController in the Route.yorbro

1 Answers

7
votes

As @blessenm said, the model hook is only for the Route. That is one of the main responsibilities of the Route object: it retrieves and sets up the data to be shown in that route.

The model property of a Controller is not meant as a hook: it is a property. The reason it is not read-only is that the Route is supposed to set and modify it.