3

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

Specifically, I'd like to get a test like this working, but for Ember CLI: e.g. 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's model function.

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

I've figured out that within functions I can do:

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

but I'm pretty sure import at the top of the file is supposed to work.

I'm experiencing this on Ember 0.40.0 (both in my existing app and a fresh app). See below for more specifics where I'm finding it undefined. Setting var ajax = icAjaxRaw outside of the functions does not work either. I'm at a bit of a loose end 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;

---> icAjaxDefineFixture IS 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 these are actually needed with the new ember-cli-ic-ajax addon):

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

1 回答 1

3

有同样的问题。原来这是一个 Chrome 调试器优化问题,请查看这篇博文 http://johnkpaul.com/blog/2013/04/03/javascript-debugger-surprises/

在调试时,如果您尝试在控制台中使用闭包范围内的变量,而该变量实际上并没有在源代码中使用,您会对 ReferenceErrors 感到惊讶。这是因为 JavaScript 调试器优化了你的代码,如果变量未使用,它们将从函数的词法环境中删除。

为了在调试器中玩耍,我刚刚ajax;在闭包内键入,变量神奇地出现了。

于 2014-10-22T00:27:13.873 回答