Skip to content
Joshua Plicque edited this page Jan 9, 2017 · 5 revisions

Useful Tricks

Accessing collection routes

Accessing a collection route is straightforward but has a bit of an awkward interface, because the collection route action is actually a method on an instance.

this.store.createRecord('fruit').citrus({param: value, param2: value2}).then( () => ... );

Add returned data to the store

If the response to your memberAction returns the updated state of the object you changed, you can update the store with the following. This assumes a component structure and a JSONAPI interface, although assuming you set up your adapters correctly, it should work with any interface.

The model:

// models/article.js
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { memberAction } from 'ember-api-actions';

export default Model.extend({
  name: attr('string'),
  body: attr('string'),

  publish: memberAction({
    path: 'publish',
    type: 'put'
  })
});

and the component:

// components/one-article/component.js
import Ember from 'ember';

export default Ember.Component.extend({
  store: Ember.inject.service(),

  actions: {
    publish() {
      this.get('article').publish().then((response) => {
        this.get('store').push(this.get('store').normalize('article', response.data));
      });
    }
  }
});
Clone this wiki locally