Getting Controller properties from Adapters

I am using ember 1.12.0 and ember-cli 0.2.7

I am following this link RESTAdapter - 4.6 - Ember API Documentation The problem is that everytime I use “this” inside a Adapter (like app/adapters/application.js) I got a error

Build error

<app name>/adapters/application

`this` at the top level is undefined

I.E. I get this error with this code:

export default DS.RESTAdapter.extend({
  headers: function() {
    return {
      "API_KEY": this.get("session.authToken"),
      "ANOTHER_HEADER": "Some header value"
    };
  }.property("session.authToken")
});

So, how can I get a controller property from a adapter?

this.container.lookup("controller:application"); See: https://github.com/broerse/ember-cli-blog/blob/master/app/adapters/application.js

actually, I can`t even use “this” inside an adapter, it always give me a build error.

this at the top level is undefined

Did you use the bind(this) as in my adapter example? Is should work.

Edit: It should work without the bind(this) in your case.

Yeah, maybe its because I am using the Ember Data? Basically, I am trying to do this:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({   headers: function() {
    return {
      "API_KEY": this.get("session.authToken"),
      "ANOTHER_HEADER": "Some header value"
    };
}.property("session.authToken") });

edit 1: this code is in the docs RESTAdapter - 4.6 - Ember API Documentation

what if you write var at = this.get("session.authToken") before the return?

Is is strange because I think it should work the way you wrote it. I will take look.

Nothing :frowning: .Ok, I will search if someone had this problem before and some source code. Thanks

1 Like

OK. It is strange code in your adapter (looks more like controller code) but it should work. I just inserted your code in a project and can’t get the error you have. If I need to check something please let me know.

This code is not mine, Its directly from the ember docs, but the problem occurs on “this” keyword. Could you tell me which version of ember-cli, ember and ember-data you are using? Mine are: ember: 1.12.0, ember-cli: 0.2.7, ember-data: 1.0.0-beta.18.

Here is the link pointing directly: RESTAdapter - 4.6 - Ember API Documentation

Ember Inspector 1.8.2 Ember 1.12.1 Ember Data 1.0.0-beta.19.2 jQuery 1.11.3

A working test: GitHub - broerse/rffaguiar

1 Like

wow, thanks. I just updated everything and now its working. Do I have a way to give you more years in your life for your help? :smiley: Really thank you!