buildURL with dynamic values


#1

Is it possible to have buildURL() observe data elsewhere? Say an ember object?

For instance, I would like to change my URL to change depending on values in other ember-data objects. This seems rather convoluted, so let me try and give an example:

Lets say my buildURL function looks like this (currently):

buildURL = function () { return ‘/api’+this._super(record,suffix) + ‘.json’ }

Now say I have a user defined option set, that I want to pass with all my requests. Something like:

myApp.customSetting = ‘value’;

My buildURL() function would look like:

buildURL = function () { return ‘/api’+this._super(record,suffix) + ‘.json?customSetting=’.myApp.customSetting }

This works fine, but only if myApp.customSetting is set correctly on the initial load. Is this even possible?


#2

I’m assuming you mean buildURL() from Ember.Data.

Isn’t that method being called whenever a request is made? That means that you don’t need bindings/observers, as the URL will be built with every call the App.Whatever.find() etc.

Please clarify if I’m missing something.


#3

@seilund Yes I do mean buildURL() from Ember.data. Sorry if I was not clear.

The issue is that is only appears to be called once, and the value stored. Though I am not quite sure why, or how to override this behavior. I was considering sending data like a user session token (to validate that the session is still valid) as well as some other use cases however everything I have tried recently fails.


#4

Hmm… https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L365 shows that buildURL doesn’t store any state. And its used like this fx https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L256 where you can see that it calls buildURL everytime.

JSBin that shows the issue?


#5

@seilund I am in the middle of upgrading my app to rc4, I am wondering if it is something in my rc3 build. I’ll try and update once i get my app running again.


#6

Is it possible to pass values from route to buildUrl?

Something like

App.ArticlesRoute = Ember.Route.extend({
  model: function() {
    var token = this.controllerFor('login').get('token');
    return this.store.findAll('article', {token: token});
  }
});

How to get this value inside buildURL? Or maybe there is a better way to do it?