How do you consume custom routes with Ember?

Asked

Viewed 56 times

0

My Rails api has the user/me route that returns the current user data, written like this:

  #routes
  resources :usuarios do
    get :mim, on: :collection
  end

  #controller
  def mim
    render json: usuario_atual
  end

How can I access this api route with Ember to get user data?

  • I found the solution here http://www.thegreatcodeadventure.com/creating-a-current-user-property-in-ember/ #grateful

2 answers

1

The simplest format would be:

// rota: user.js
import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return Ember.$.ajax('http://host/user/min');
  }
});

And in the user.Hbs template

Nome: {{model.name}}
  • It is not managed by Ember-data using ajax call.

0

You must create a adapter for your model. It can customize on the way.

import DS from 'ember-data';
import { underscore } from '@ember/string';

export default DS.JSONAPIAdapter.extend({
  pathForType(type) {
    return '/autenticacao';
  }
});

In your route then you search the model

model() {
    return this.store.findeRecord('me');
    // ira fazer uma requisicao para host/autenticacao/me

}

https://guides.emberjs.com/release/models/customizing-adapters/#toc_path-customization

Browser other questions tagged

You are not signed in. Login or sign up in order to post.