Skip to content

Instantly share code, notes, and snippets.

@commadelimited
Created October 1, 2013 14:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commadelimited/6779774 to your computer and use it in GitHub Desktop.
Save commadelimited/6779774 to your computer and use it in GitHub Desktop.
Nested URLs
I'm trying to consume an endpoint from my server to retrieve interactions.
This interaction is restricted by account and can only be accessed once you're within the account route.
URLs
----
/activity/#/accounts/64/ <-- loads account information
/activity/#/accounts/64/interactions <-- I want to load interactions here
Ember is making a call to the interactions endpoint, but it's hitting the wrong path:
API
---
/api/1/interactions/ <-- what Ember is hitting
/api/1/accounts/64/interactions/ <-- what Ember should be hitting
Is there something I can do to alter the endpoint that Ember is trying to hit? Below are the details on my stack:
DEBUG: Ember.VERSION : 1.0.0-rc.8
DEBUG: Ember.Data.VERSION : r13
DEBUG: Handlebars.VERSION : 1.0.0
DEBUG: jQuery.VERSION : 2.0.3
Social.Router.map(function() {
this.resource('accounts', { path: '/accounts' }, function(){
this.resource('account', { path: ':account_id'}, function(){
this.resource('interactions', { path: 'interactions'});
});
});
});
@ryanflorence
Copy link

I'm not sure why everybody is posting application routes, because they are not the routes your models are going to use.

In new ED beta 1.0 you get per model adapters/serializers. So it'd look something like this:

App.Interaction = DS.Model.extend();

App.Account = DS.Model.extend({
  interactions: DS.hasMany('interaction', {async: true})
});

App.AccountSerializer = DS.RESTSerializer.extend({
  normalize: function(type, hash, property) {
    // want to construct a url like this:
    // /api/1/accounts/64/interactions
    var base = '/api/1/accounts/'+hash.id;
    hash.links = {
      // if your server doesn't send this links key, we can do it
      // ourselves and shove it back into ED
      interactions: base+'/interactions'
    };
    return this._super(type, hash, property);
  }
});

App.InteractionsRoute = Ember.Route.extend({
  model: function() {
    // since your interactions route is nested in `account`, we just
    // ask for the relationship, when it lands, the data will populate
    return this.modelFor('account').get('interactions');
  }
});

@commadelimited
Copy link
Author

Ryan, that's the question I have. How do I tell the Model where to look for it's data? Is that possible in r13?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment