2

I have nested route with a hasMany relationship. I have declared the model this way:

export default DS.Model.extend({
    label: DS.attr('string'),
    archetyp: DS.attr('number'),
    searchable: DS.attr('boolean'),
    showInList: DS.attr('boolean'),
    managedItem: DS.belongsTo('managedItem')
});

And the corresponding model looks like so:

export default DS.Model.extend({
  title: DS.attr('string'),
  description: DS.attr('string'),
  logo: DS.attr('string'),
  logo_alt: DS.attr('string'),
  fields: DS.hasMany('field', {async: true})
});

And in the fields/index route I want to load the fields from the server. Therefore I have hooked into the model hook.

export default Ember.Route.extend({
  model() {
    let fields = this.modelFor('managedItems/edit').get('fields');
    if (fields.get('isFulfilled')) {
      fields.reload();
    }
    return fields;
  }
});

But I can't see any network request and if I try to console.log(field) I see that the model is empty. What do I miss here?

EDIT: As an additional information: if I just call this.modelFor("managedItems/edit") and ask for some properties of the model object, I always get undefined except for the isLoaded property...
Here is the Router:

Router.map(function() {
  this.route('managedItems', {
  }, function() {
    this.route('new');
    this.route('show', {path: ':managedItem_id'});
    this.route('edit', {path: ':managedItem_id/edit' },
      function() {
           this.route('fields', { resetNamespace: true }, function () {
         });
      });
    });
});
ykaragol
  • 6,139
  • 3
  • 29
  • 56
Shimu
  • 1,137
  • 11
  • 25
  • please provide some more detail. code examples. what have you already tried? the search field "in another template".. where exactly? – Grapho Apr 04 '16 at 14:22
  • I kind of fixed this issue with a store.query and retrieve the fields this way... But any help would be appreciated since I think the update of the related model won't happen this way... – Shimu Apr 21 '16 at 13:49

2 Answers2

0

Your model hook in the route should return a Promise in any case. Have you tried something like:

export default Ember.Route.extend({
    model() {
        return this.modelFor('managedItems/edit').get('fields');
    }
});
Nazim
  • 367
  • 2
  • 11
0

Change managedItems/edit to managedItems.edit as below:

let fields = this.modelFor('managedItems.edit').get('fields');

"managedItems/edit" is the path of the route (if you don't overwrite it). The name of the route is "managedItems.edit".

Also, fields.reload(); is returning a promise. I'm not sure but IMO the correct model hook function will be like that:

export default Ember.Route.extend({
  model() {
    let fields = this.modelFor('managedItems.edit').get('fields');
    if (fields.get('isFulfilled')) {
      return fields.reload(); //You should return this promise.
    }
    return fields;
  }
});
ykaragol
  • 6,139
  • 3
  • 29
  • 56
  • Tried this, but it is not working... No request is created – Shimu Apr 26 '16 at 06:57
  • Do you again get undefined for modelFor? – ykaragol Apr 26 '16 at 07:00
  • 1
    No, this time I get an actual class. :-) Now I need to find out how to get the related models... – Shimu Apr 26 '16 at 07:04
  • What is returning from `this.modelFor('managedItems.edit').get('fields')`? A promise? – ykaragol Apr 26 '16 at 08:27
  • It does look this way... At least the property "isFulfilled" is set to true. – Shimu Apr 26 '16 at 09:00
  • Edit: If I take a look in the console.log(fields) the isFulfilled property is set to true, but if I ask it with fields.get("isFulfilled") I get false. But again, Ember isn't creating any requests to the server... – Shimu Apr 26 '16 at 09:14
  • Any ideas why I don't get a request/data? – Shimu Apr 26 '16 at 10:45
  • Nope. Maybe suggest you to have a look at [this post](http://stackoverflow.com/questions/19983483/how-to-reload-an-async-with-links-hasmany-relationship) – ykaragol Apr 26 '16 at 11:02