Data: Headers in JSONAPIAdapter not working as described

Created on 5 Jan 2018  路  5Comments  路  Source: emberjs/data

Following exact instructions on
Ember site

generates the following ERROR

8:6 error Only string, number, symbol, boolean, null, undefined, and function are allowed as default properties ember/avoid-leaking-state-in-ember-objects

Configuration

ember-cli: 2.18.0
node: 4.8.4
os: darwin x64


import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
     headers: {
          'Authorization': 'XXX'
     }
});
Bug

Most helpful comment

I've tracked down the issue to the build time avoid-leaking-state-in-ember-objects rule in the eslint-plugin-ember plugin. Which is trying to prevent multiple ember objects from modifying a shared object. This usually isn't an issue for headers since most ember applications only have 1 instance of any given adapters but we should probably update the documentation to recommend using Ember.computed as the default patter for defining headers.


import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
     headers: Ember.computed(function() {
         return {
            'Authorization': 'XXX'
         }
     })
});

All 5 comments

What version of Ember are you using @adrianboston?

I've tracked down the issue to the build time avoid-leaking-state-in-ember-objects rule in the eslint-plugin-ember plugin. Which is trying to prevent multiple ember objects from modifying a shared object. This usually isn't an issue for headers since most ember applications only have 1 instance of any given adapters but we should probably update the documentation to recommend using Ember.computed as the default patter for defining headers.


import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
     headers: Ember.computed(function() {
         return {
            'Authorization': 'XXX'
         }
     })
});

@bmac that would also throw a linting error as a bad practice since there is no dependent get :P

Setting headers in init until we have constructors is likely the best recommendation.

cc @locks and @rtablada for learning team

Closing in favor of the discussion in #4496

Was this page helpful?
0 / 5 - 0 ratings