0
votes

I'm trying to make a search inside a nested route particularly that shares model.

Here's my code example

Products and Search returns a json

Router

Market.Router.map ->
    @resource 'products'
    @resource 'search', { path: "products/search" }
    @resource 'product', { path: "products/:product_id" }

The rest adapter api url I'm trying to achieve is the following:

http://api.url:3000/v1/products/search?search_terms="rope"

I have an action inside a header menu controller for the search

How do I find records inside a nested route?

Market.HeaderMenuController = Ember.ArrayController.extend({
    searchText: null,
    actions: {
        searchResults: function(){
             this.store.find('product', 'search?search_terms='+this.searchText);
             this.store.find('search', { "search_terms":this.searchText });
        }
    }
});

I get the following errors -

Error: Assertion Failed: You must include an id for Market.Product in an object passed to push

Uncaught Error: Assertion Failed: Error: Assertion Failed: You must include an id for Market.Product in an object passed to push

2

2 Answers

0
votes

The error you are seeing is caused by the JSON being returned from your this.store.find method. Ember-data expects all model objects to have an id property specified. If you inspect the JSON that is returned from your API endpoint, you will probably find there is no id property on the object.

Make sure that the JSON you're returning has an id property defined (on each 'product' object) and the error should be resolved.

0
votes

This was my temporary approach, probably there are better solutions out there.

In order to achieve a nested resource for search, you can do the following.

The api endpoint was

http://api.url:3000/v1/products/search?search_terms="rope"

I create a custom adapter for search route

Market.SearchAdapter = Market.ApplicationAdapter.extend(
  namespace: "v1/products"
)

A custom pluralization for search, and avoid having "searches" endpoint

Ember.Inflector.inflector.rules.uncountable['search'] = true;
inflector = Ember.Inflector.inflector;
inflector.irregular('search', 'searches');

Now you can run a store find for search

this.store.find('search', { 'search_terms': this.searchText });

With the previous endpoint.