Why is ic-ajax not defined in certain functions in the Ember CLI?

Forgive my ignorance, but I can't get ic-ajax to work inside certain functions.

In particular, I would like to get a test like this how it works, but for the Ember CLI: for example http://coderberry.herokuapp.com/testing-your-ember-application#30

I can call ajax inside Ember.Object.Extend and outside of functions and object definitions, but not in modules, tests, or Ember.Route model function.

Am I misunderstanding something or is there a misconfiguration in my application?

I realized that within functions I can do:

ajax = require('ic-ajax')['default'];
defineFixture = require('ic-ajax')['defineFixture'];

      

but I'm pretty sure the import

top of the file should work.

I am testing this on Ember 0.40.0 (in both my existing app and new app). See below for details where I find it undefined. Setting var ajax = icAjaxRaw

outside of functions also doesn't work. I'm a bit frustrated, so any help you could give in this regard would be great.

users-test.js:

import ajax from 'ic-ajax';
import { raw as icAjaxRaw } from 'ic-ajax';
import { defineFixture as icAjaxDefineFixture } from 'ic-ajax';

debugger;

      

---> icAjaxDefineFi Defined here

module('Users', {
  setup: function() {

    App = startApp();
    debugger;

      

icAjaxDefineFixture β†’ UNDEFINED

  },
  teardown: function() {
    Ember.run(App, App.destroy);
  }
});

test("Sign in", function() {

      

icAjaxDefineFixture β†’ UNDEFINED

  expect(1);
  visit('/users/sign-in').then(function() {
    equal(find('form').length, 1, "Sign in page contains a form");
  });
});

      

Brocfile.js (I don't think this is really necessary with the new admin ember-cli-ic-ajax):

app.import('vendor/ic-ajax/dist/named-amd/main.js', {
  exports: {
    'ic-ajax': [
      'default',
      'defineFixture',
      'lookupFixture',
      'raw',
      'request',
    ]
  }
});

      

+3


source to share


1 answer


There was the same problem. Turns out this is a Chrome debugger optimization issue, check this blog post http://johnkpaul.com/blog/2013/04/03/javascript-debugger-surprises/

While debugging, if you try to use a variable from the close scope in the console that was not actually used in the source, you will be surprised at ReferenceErrors. This is because JavaScript debuggers optimize the hell out of your code and remove variables from the Lexical environment of a function when not in use.



To play in the debugger, I just typed ajax;

inside the closure and the variable magically appeared.

+3


source







All Articles