RESTAdapter load custom URL


#1

I have a route which returns a model that I want to pull in from my app’s RESTAdapter. For a normal model, I would make a find call to the DS as follows:

model: function () {
    return this.store.find('listing'),
}

This works great for my backend endpoint GET /listings. However, my backend also serves a GET /listings/unpublished endpoint, which I would also like access to. In pseudo-code, what I’m trying to achieve is something that looks like

model: function () {
    return this.store.find('listing/unpublished'),
}

I see four possible solutions:

  1. Override the RESTAdapter default methods somehow, or add a new method that allows me to build custom URLs

  2. Directly call the ajax method on the RESTAdapter with custom parameters. This would be ideal, although I can’t get access to it:

     App.ApplicationAdapter = DS.RESTAdapter.extend({ ... });
     console.log(App.ApplicationAdapter.ajax)
    

    returns undefined

  3. Write my own adapter that borrows very heavily from the RESTAdapter

  4. Fall back to a jQuery $.get(’/listings/unpublished’) request

I’m not advanced enough in Ember to judge which is best, so would appreciate the opinion of a veteran. Thanks in advance for the help.


#2

you need to create 1 custom adapter for this model ( http://emberjs.com/guides/models/customizing-adapters/ )

and overwrite the pathForType method in it.


#3

Thanks @schwaiger, I’ll give it a try :slight_smile:


#4

I’ve been struggling with trying to get a pathForType solution work for a while now. The only way I have come up with is doing something like this.store.find('listings:unpublished'), and doing some funky string manipulation in the pathForType function. I haven’t been able to get this to work, however. How would you suggest I do it?


#5

Why don’t you create a "new model’ that’ exactly the same as the old one just to get the right path.

App.UnpublishedListing = App.Listing;   // use extend if something was different between those

App.UnpublishedListingAdapter = DS.RESTAdapter.extend({
  pathForType: function () { return 'listing/unpublished'; })
});

This should work for POST and GET collection. Not sure if DELETE and GET item’s are done under ‘listing’ or ‘lsiting/unpublished’