0
votes

I have a dynamic segment route like this -

@resource 'owners', { path: "/:owner_id"}
@resource 'product', { path: "products/:product_id" }

Product route needs information from this route, and needs to load after some product params are loaded

Market.ProductRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('product', params.product_id);
    },
    afterModel: function(model){
        this.store.find('owner', model.get('id'), { 'owner_type':model.get('owner_type')});
    },
});

And I get the following errors:

Error while processing route: product Assertion Failed: metaForProperty() could not find a computed property with key 'owner_type'. Error: Assertion Failed: metaForProperty() could not find a computed property with key 'owner_type'

Error: More context objects were passed than there are dynamic segments for the route: error

Uncaught Error: Assertion Failed: Error: More context objects were passed than there are dynamic segments for the route: error

1

1 Answers

0
votes

Looking at your code, I think your intent is to load the associated owner after you load the product. I'm also assuming that you want to send the product id and owner_type to your server because this is a polymorphic relationship.

You're getting the error about owner_type error because you're passing something really weird as the third argument to find which is reserved for indicating data and relationships that you know are already preloaded -- probably not what you want. I can't help but notice the mix of CoffeeScript and JavaScript. It makes me think that you're mistaken about how find works because you're used to having CoffeeScript gather all the key-value pairs that you pass to a function into one object literal.

The second error More context objects were passed... is probably because you have a link where you're passing objects to a {{link-to}} helper when there isn't a dynamic segment (or not enough dynamic segments) in your router path. This is hard to tell from just the posted code.

Here is what I think you want to do in your product route:

Market.ProductRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('product', params.product_id);
  },

  afterModel: function(model) {
    this.store.find('owner', {
      owner_id: model.get('id'),
      owner_type: model.get('owner_type')
    });
  },
});

If you have control over your API it might be nice to just include the owner when you search for the product. I've had applications where sometimes I want associations and sometimes I don't. I often end up with routes that look like this:

Market.ProductRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('product', {id: params.product_id, include: ['owner']});
  }
});

At that point your server would take this {include: 'owner'} data to mean that it should also return the owner as side-loaded data.

Hope something in there helps!