Note that there are some explanatory texts on larger screens.

plurals
  1. POMake ember-data use the URL that ember is loading for the REST request
    primarykey
    data
    text
    <p>On the system I am working on right now, I have had to try to tame ember-data a bit about how it does its REST request. The way that ember-data by default figures the URL for a certain request for a model is just not gonna cut it with the backend I am using.</p> <p>What I need is, to get ember-data to use the <em>same</em> URL that ember is loading, but with a '?json' suffix. That is, if ember switches page to my band page, and the url is /bands, I want ember-data to request /bands?json for the data it needs, not whatever it figures from the name of the model. One could say, that I wanted the URL to be calculated from the path of the loading route, instead of from the name of the model being used.</p> <p>I have tried by subclassing DS.RESTAdapter{} and see if I could get the buildURL method to do this, but I can't figure out how to get the URL ember is gonna load. The buildURL method is called before ember changes the location, so I can't use document.location.href or something. I can imagine I will need a way to ask ember what it is now loading, and what the URL is.</p> <p>Any ideas of how to do this?</p> <p><strong>UPDATE</strong></p> <p>There hasn't been any satisfying solutions, so I decided to just do it the dirty way. This is it:</p> <pre><code>App.RouterSignature = [ ['index', '/', '/index_models'], ['bands', '/bands', '/band_models'], ['band', '/band/:band_slug', '/band_model'] ]; App.Router.map(function() { for (var i = 0; i &lt; App.RouterSignature.length; i++) { var route = App.RouterSignature[i]; this.resource(route[0], {path: route[1]}); } }); App.CustomAdapter = DS.RESTAdapter.extend({ buildURL: function(record, suffix) { var url, suffix = '?json', needle = this._super(record); for (var i = 0; i &lt; App.RouterSignature.length &amp;&amp; !url; i++) { var route = App.RouterSignature[i]; if (route[2] == needle) url = route[1]; } return url + suffix; } }); </code></pre> <p>Now App.Routes and DS.RESTAdapter.buildURL are based off the same data. The first two values in the App.RouterSignature list is just the name of the route, the path of the route. The third value is what DS.RESTAdapter.buildURL by default guesses should be the url. My custom adapter then takes that guess, matches it with one of the items in the App.RouterSignature list and then takes the second value from that item - the routes path.</p> <p>Now the requests that ember-data makes is to the same url as the routes path.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload