zapper
zapper

Reputation: 73

what is the difference between model hook in controller and route?

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?

Upvotes: 3

Views: 455

Answers (1)

yorbro
yorbro

Reputation: 1137

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.

Upvotes: 7

Related Questions