【问题标题】:Ember App Kit with ember data带有 ember 数据的 Ember App Kit
【发布时间】:2013-09-11 13:31:28
【问题描述】:

我正在尝试使用 ES6 使用 ember app kit 和 ember 数据开始一个新项目。我已经设法在adapter.js中使用以下代码创建了一个商店

var ApplicationAdapter = DS.FixtureAdapter.extend();
export default ApplicationAdapter;

但是,我无法创建模型并访问它。在models/account.js 我有这个

var Account = DS.Model.extend({
  name: DS.attr('string')
});

Account.FIXTURES = [
  {
    'id': 1,
    'name': 'Acc 1'
  }, {
    'id': 2,
    'name': 'Acc 2'
  }
]

export default Account;

在我的routes/accounts.js 我有这个:

var AccountsRoute = Ember.Route.extend({
  model: function() {
    var store = this.get('store');
    return store.find('account');
  }
});
export default AccountsRoute;

在这个阶段,我只是想从屏幕上显示的灯具中获取帐户列表。该路线运行良好,如果我输入静态数据(如索引路线),那么一切正常。但是,使用上面的代码,我遇到了麻烦

DEPRECATION: Action handlers contained in an `events` object are deprecated in favor of putting them in an `actions` object (error on <Ember.Route:ember352>)
    at Object.triggerEvent (http://localhost:8000/vendor/ember/index.js:30519:13)
    at trigger (http://localhost:8000/vendor/ember/index.js:29641:16)
    at handleError (http://localhost:8000/vendor/ember/index.js:29903:9)
    at invokeCallback (http://localhost:8000/vendor/ember/index.js:8055:19)
    at null.<anonymous> (http://localhost:8000/vendor/ember/index.js:8109:11)
    at EventTarget.trigger (http://localhost:8000/vendor/ember/index.js:7878:22)
    at http://localhost:8000/vendor/ember/index.js:8180:17
    at Object.DeferredActionQueues.flush     (http://localhost:8000/vendor/ember/index.js:5459:24)
    at Object.Backburner.end (http://localhost:8000/vendor/ember/index.js:5545:27) index.js:394
Error while loading route: 
Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function,   setRequestHeader: function, overrideMimeType: function…}
 index.js:394
Uncaught #<Object> index.js:30566

我哪里出错了?

【问题讨论】:

    标签: ember.js ember-data ember-app-kit


    【解决方案1】:

    您的Account 模型使用DS.RESTAdapter 而不是DS.FixtureAdapter,因为您在ApplicationAdapter 中设置了适配器,因此预期为AccountAdapter。所以你从 ajax 收到一个错误,可能是因为 url 与服务器不匹配。

    要为每个模型配置 DS.FixtureAdapter,请使用:

    var AccountAdapter = DS.FixtureAdapter.extend();
    export default AccountAdapter; 
    

    或作为所有模型的全局适配器:

    App.Store = DS.Store.extend({
        adapter: DS.FixtureAdapter
    });
    

    希望对你有帮助

    【讨论】:

    • 我正在使用fixtureadapter。几乎和你建议的完全一样
    • 对不起。我的意思是,由于配置错误,您正在使用其余适配器。但是你想使用夹具...
    • 我没有使用其余适配器,我使用的是夹具适配器。除非我错过了什么!?我没有在哪里定义重新适配器
    • 其余适配器是默认的。所以 ember 正在执行以下操作:尝试从模型中获取适配器,在您的情况下为 AccountAdapter。未找到 AccountAdapter。现在使用全局适配器,默认适配器没有更改,因此其余部分将使用。我在答案中的第一个示例显示了如何为每个模型设置适配器,第二个示例是如何覆盖默认适配器。
    • 我尝试了第一个,但没有成功。接下来我使用了你的全局适配器(这是我首先尝试的)。它工作得很好。出于某种原因,我在文档中读到 ember 1.0 中可以使用商店时感到困惑。对于它的价值,我只是在 app.js 中添加了适配器代码。我不确定那是否是正确的地方,但它有效。如果没有,请赐教。
    【解决方案2】:

    我认为真正的问题是您在 adapters/adapter.js 中定义了您的 Fixture Adapter。

    当你打电话时:

    store.find('account');
    

    它正确找到了模型,但随后又寻找了正确的适配器。您没有 adapters/account.js,所以它使用了应用程序默认值,已经提到的是 RESTAdapter。

    要让您的示例正常工作,只需更改文件名。

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误......

      但是我能够通过导入我的 ApplicationAdapter 并使用它来定义我的商店来解决此问题:

      应用程序/适配器/应用程序.js:

      var ApplicationAdapter = DS.FixtureAdapter.extend();
      
      export default ApplicationAdapter;
      

      app/store/application.js:

      import ApplicationAdapter from 'appkit/adapters/application';
      var Store = DS.Store.extend({
          adapter: ApplicationAdapter
      });
      
      export default Store;
      

      请记住,我尚未将默认应用程序名称从 appkit 中更改出来,您可能需要更改此名称或路径才能使此功能正确地为您服务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 2013-04-19
        相关资源
        最近更新 更多