Getting Controller properties from Adapters


#1

I am using ember 1.12.0 and ember-cli 0.2.7

I am following this link http://emberjs.com/api/data/classes/DS.RESTAdapter.html 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?


#2

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


#3

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

this at the top level is undefined


#4

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.


#5

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 http://emberjs.com/api/data/classes/DS.RESTAdapter.html


#6

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


#7

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


#8

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


#9

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.


#10

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: http://emberjs.com/api/data/classes/DS.RESTAdapter.html#toc_headers-customization


#11

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


#12

A working test: https://github.com/broerse/rffaguiar


#13

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!