Search code examples
ember.jsember-dataember-cli

set alias for `model` hook


HELP

If there is a model hook in app/routes/post.js say

model() {
  return this.store.query('post');
}

in template the returned promised is accessed using

{{#each model as |post|}}
  ...
{{/each}}

Is there any way to set alias for the model? Something like this in route or controller?

posts: alias('model')

So I can access the returned promise in the template as

{{#each posts as |post|}}
  ...
{{/each}}

Is this something which is already present or something that got missed from ember documentation?


Solution

  • you can create alias for model property in your controller,

    import Controller from '@ember/controller';
    import { alias } from '@ember/object/computed';
    
    export default Controller.extend({
      posts: alias('model')
    })
    

    or using setupController in your route,

    export default Route.extend({
      setupController(controller, model) {
        controller.set('posts', model);
      },
    });
    

    Reference:

    alias api documentation - alias computed property

    alias your model - alias-model-rule