Ember-cli , how do i change rest call on fly in the rest adapter

343 views Asked by At

Im working on ember-cli, how do i change rest call on fly in the rest adapter. If i use path params not query params?for example:

export default DS.RESTAdapter.extend({   
  namespace:'res/v1/users/id',   
  pathForType: function() {  
       return Ember.String.underscore("friends");},});

Based on the user selection from dropdown we get the "id", using the id I need to get user friends from the database. Could you please suggest a better way to do. My aapplication supports pathparams not the query params

3

There are 3 answers

0
Sameeksha Chilukuri On BEST ANSWER

I have created a variable in enviroment.js 'userId'. When ever i select a user i set config.userId in the controller to the corresponding Id.

config.userId=this.get('selectedUser');

In pathforType of adapter I used this varible

 pathForType: function() {
    return Ember.String.underscore(config.userId+"/friends");
}

you just need to add an import statement

import config from '../config/environment';

Please suggest me if anyone get to know better way. Thanks all for your responses

0
Gabe Rainbow On

buildURL() only takes the type imo. so you have to pass some more jazz.

i did something along the lines of the following in the application adapter

$ ember generate adapter application

app/adapters/application.js

import DS from 'ember-data';

export default DS.RESTAdapter.extend({

    findQuery: function(store, type, query) {

        var urlQuery = query.theshityouwant;

        var reply = this.ajax(this.buildURL(type.typeKey + '/' + urlQuery), 'GET', { headers: all});
        return reply;
    },
})

});

1
Andrey Mikhaylov - lolmaus On

To customize the URL, override the buildURL method in your adapter.

The tricky part is to access related records from the adapter. For example, you request friends for a given user. You work in a friend adapter, but you need to know the user's id to include it in the URL.

For that purpose, use the record property on the snapshot argument of the buildURL method.

Alternatively, you might want to override some of buildURL's underlying methods such as urlForFindQuery, depending on how you request your model from the store. With a find.query(), you will retrieve the id of the user from the query.

If this does not help you, please respond with the way you're trying to fetch friends from the store.