0
votes

I have in my router.js:

Router.map(function() {
  this.route('portfolio', function() {
    this.route('company', { path:'/company/:id' });
  });
}

And in my routes/portfolio/company.js:

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(params) {
    var companyId = params.id;
    return new Ember.RSVP.hash({
            company: Ember.$.ajax({ url: '/api/company/'+companyId, dataType: "json", type: 'GET' })
        }).then(function(message) {
            return message;
        }, function(error) {
            console.log( error );
        });
    }
});

My route and template is loading fine, when I navigate to app/portfolio/company/1, but for some reason when I navigate to that route, Ember wont load the model (no error, but the {{model}} variable does not get populated in template). Only when I refresh the page, Ember loads the model?! I am a bit confused now...

Edit: added missing param and added better description

1
" when I navigate to that route, Ember wont load the model" - what does that mean? Is there some error in console, blank page or what? - Daniel Kmak
I added better description, but basically model function does not get called and {{model}} variable in template does not get populated - Maksim Luzik
Model function won't get called if you are passing a model to your link-to helper, pass the model.id instead. - Patsy Issa
Thanks Kitler! Will do that. - Maksim Luzik

1 Answers

0
votes

I think in your template or in controller you are using model like so model.company replace it with model, and remove extraneous RSVP.hash because Ember.$.ajax already returns promise which model hooks can handle so in ES6 (ember-cli supports it) your model hook should look like this

model({ id }) {
  return Ember.$.ajax('/api/company/' + id);
}

with above things everything should work, what was happening I think you were passing just model to {{link-to}} while your controller or template expecting model.company so was breaking things