jeudi 3 septembre 2015

Override the "namespace" part of the URL for Ember Data's RESTAdapter

This is for Ember.js v 1.11 and Ember Data 1.0.0-beta.16.1

I am adding new functions to an existing application but the API for the new REST endpoints are not yet implemented on the server so I want to use dummy data in local files.

To avoid cross-domain issues my server's REST API is proxied locally using Nginx such that http://localhost/my_server_api is proxied to the remote server. This allows me to test my app locally with the data on the remote server. For example: http://localhost/my_server_api/apple serves the data for my App.Apple model.

However, for the new models I am introducing I want to be able to change my_server_api to my_test_files like this: http://localhost/my_test_files/banana

Where the model will be coded like this:

App.Banana= DS.Model.extend({
  uuid: DS.attr('string'),
  description: DS.attr('string')
});

There is only one adapter in the project that looks like this:

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

There is no explicit setting of the namespace for this adapter. I'm new to Ember, and new to this project so I'm guessing that it derives from this global setting in my app.js:

window.namespace = "my_server_api";

So I think my problem is how to set the namespace for the adapter to be "my_test_files" for the App.Banana model.

I have tried the following:

App.BananaAdapter = App.ApplicationAdapter.extend({
    namespace: "my_test_files");
});

but it made no difference. When I test it in the console using this:

App.__container__.lookup('store:main').find('banana');

I see it is still trying to access the global namespace like this http://localhost/my_server_api/banana

I tried registering a new adapter as described here but I there is no explicit store being instantiated in my project, and when I enter this into the console:

App.__container__.lookup('store:main').registerAdapter('App.Banana' ...);

it tells me that registerAdapter is not defined.




Aucun commentaire:

Enregistrer un commentaire