dimanche 5 juillet 2015

Use a nested route for fetching data from the server

I'd like to ask a RESTful server for just the products of a given category. The server has a nested route for both. So the path /categories/1/products will export only the products of the category with the ID 1.

In my current application I has a route which fetches the first category and all products. Which is a waste of resources. How can I change it that Ember Data fetches only the products of that category using the nested path of the server?

route.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return {
      category: this.store.find('category', 1),
      products: this.store.find('product')
    };
  }
});

app/category/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  products: DS.hasMany('product', { async: true })
});

app/product/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string')
});

app/application/adapter.js

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  host: 'http://www.example.com',
  namespace: 'api/v1',
});




Aucun commentaire:

Enregistrer un commentaire