0
votes

say i have the models custom_route and user. to get the data for the routes that belong to the user ajax requests that look like "/users/:user_id/routes" have to be send to the server. now .. how do i get ember-data to send these get requests?

When i setup the specific route related to this request like this:

App.UserCustomRoutesRoute = Em.Route.extend({ model: function() {

        return this.store.find('user_custom_route');      });

... the request is just '/user_custom_route'

i took a look at the transition cheat sheet for beta 1 (https://github.com/emberjs/data/blob/master/TRANSITION.md) but it didn't really help me.

1

1 Answers

0
votes

Update below

I found a solution but I don't know if it is really the way to go. First I setup the route with a findQuery call:

App.UserCustomRoutesRoute = Em.Route.extend({
        model: function() {
            return this.store.find('user_custom_route', {user_id: this.controllerFor('user').get('user_id')});
        }
});

Then I hooked into the adapter methods which configure the get request (findQuery and buildUrl).

App.UserCustomRouteAdapter = DS.RESTAdapter.extend({
        buildURL: function(type, id) {

            return 'users/' + id + '/routes';
        },
        findQuery: function(store, type, query) {
            return this.ajax(this.buildURL(type.typeKey, query.user_id), 'GET');
        }
    });

Setup like this the request has the form of 'users/:user_id/routes'.

Update

I had to change the model method of the App.UserCustomRoutesRoute. Most of the times user_id at App.UserController isn't yet set when App.UserCustomRoutesRoute is called (p.e. when a user enters the site via its related address ('url#/users/:user_id/custom_routes'). So I had to put a authentication check which replies the user_id if successful before the call to the related store.

 App.UserCustomRoutesRoute = Em.Route.extend({
        model: function() {
            var self = this;
            console.log('user custom routes route');
            return Em.$.get('loggedIn').then(function(promise) {
                return self.store.find('user_custom_route', {user_id: promise.user_id});
            });
        } 
});