How to access a routes model in Ember 2

JB2

I am having some trouble accessing my route's model inside my template. My model is created successfully as part of an action defined in my home controller:

actions: {
    createProject: function () {
        var project = this.store.createRecord('project', {
            name: 'Sample name'
        });
        var self = this;
        var promise = project.save();

        promise.then(function (response) {
            self.transitionToRoute('estimate', project);
        });
        promise.catch(function (errors) {
            console.log(errors);
        });
    }
}

The model's uuid is correctly serialized into the URL, with the URL inside my router.js being as follows:

  this.route('estimate');
  this.route('estimate', { path: '/estimate/:project_id' });

My estimate controller also correctly specifies the project dependency:

import Ember from 'ember';

export default Ember.Controller.extend({
    needs: ['project']
});

...and I define the model inside my routes/estimate.js as follows:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
    // model: function(params) {
    //     return this.store.find('project', params.project_id);
    // },
    model(params) {
        return this.store.findRecord('project', params.project_id);
    }
});

Logging the output of this.store.findRecord('project', params.project_id) I get:

Class {__ember1458995391969: null, __ember_meta__: Meta}
__ember1458995391969
...

However when I try to access the project model inside my estimate handlebars template (using {{model.project.name}}), I get nothing (no errors, no output).

Anybody have any thoughts on this?

Alisdair McDiarmid

The return value of the model hook is bound to the model property of the controller, which binds into the template under the same name.

You should write {{model.name}}, not {{model.project.name}}.

Background

Your model hook is returning an Ember Data promise object directly (which is normal). This means that model refers to your project object; there is no model.project property.

Rendering {{model.project.name}} is like calling this.get('model.project.name') in your controller. Ember will return undefined for this path, even if model.project is undefined itself:

> $E.get('model')
< Class {store: Class, isLoaded: true, manager: Class, isUpdating: false, __ember1459002119210: "ember610"…}
> $E.get('model.project')
< undefined
> $E.get('model.project.name')
< undefined
> $E.get('model.project.name.whatever')
< undefined

So there's no error in this case. And nothing shows up in the template, because null and undefined are rendered as blank strings.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access model array by index in Ember

From Dev

How to access model data in Ember controller

From Dev

How to access data from routes in an Ember application template?

From Dev

How to access Ember Model from the Route in Ember CLI

From Dev

How to prevent access to specific routes in Symfony 2

From Dev

How to access model-specific constant in routes.rb

From Dev

How to nest two routes in Ember?

From Dev

How to nest two routes in Ember?

From Dev

How to correctly access Ember model data from controller

From Dev

(Ember.js) How to Share Model Data Among Unrelated Resources/Routes?

From Dev

Ember access model inside a controller

From Dev

Ember: How to underscore a Model

From Dev

Ember 2.7 model inheritance - how to access data in the template that comes from a model related to parent?

From Dev

Ember.js 2 How to filter an existing model?

From Dev

How to configure Ember routes in a Chrome extension?

From Dev

How to use one view for multiple routes in Ember

From Dev

How to do Ember unit testing with asynchronous routes?

From Dev

How to link to each nested routes in ember?

From Dev

How to properly generate resources and routes with Ember CLI

From Dev

How to link to each nested routes in ember?

From Dev

using another routes model in the controller in ember.js

From Dev

Access model from controller in ember.js

From Dev

Ember: Access sideloaded model relationship data in template

From Dev

how to read model in afterModel in ember

From Dev

how to read model in afterModel in ember

From Dev

Ember - how to represent this json in a model

From Dev

How do I access the model data from the controller to render in the view in Ember?

From Dev

How do I access the model data from the controller to render in the view in Ember?

From Dev

Play 2 - Access conf property in routes file

Related Related

  1. 1

    How to access model array by index in Ember

  2. 2

    How to access model data in Ember controller

  3. 3

    How to access data from routes in an Ember application template?

  4. 4

    How to access Ember Model from the Route in Ember CLI

  5. 5

    How to prevent access to specific routes in Symfony 2

  6. 6

    How to access model-specific constant in routes.rb

  7. 7

    How to nest two routes in Ember?

  8. 8

    How to nest two routes in Ember?

  9. 9

    How to correctly access Ember model data from controller

  10. 10

    (Ember.js) How to Share Model Data Among Unrelated Resources/Routes?

  11. 11

    Ember access model inside a controller

  12. 12

    Ember: How to underscore a Model

  13. 13

    Ember 2.7 model inheritance - how to access data in the template that comes from a model related to parent?

  14. 14

    Ember.js 2 How to filter an existing model?

  15. 15

    How to configure Ember routes in a Chrome extension?

  16. 16

    How to use one view for multiple routes in Ember

  17. 17

    How to do Ember unit testing with asynchronous routes?

  18. 18

    How to link to each nested routes in ember?

  19. 19

    How to properly generate resources and routes with Ember CLI

  20. 20

    How to link to each nested routes in ember?

  21. 21

    using another routes model in the controller in ember.js

  22. 22

    Access model from controller in ember.js

  23. 23

    Ember: Access sideloaded model relationship data in template

  24. 24

    how to read model in afterModel in ember

  25. 25

    how to read model in afterModel in ember

  26. 26

    Ember - how to represent this json in a model

  27. 27

    How do I access the model data from the controller to render in the view in Ember?

  28. 28

    How do I access the model data from the controller to render in the view in Ember?

  29. 29

    Play 2 - Access conf property in routes file

HotTag

Archive