5

I'm trying to start a new project with ember app kit and ember data using ES6. I've managed to create a store using the following code in adapter.js

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

However, I'm failing to create a model and access it. In models/account.js I have this

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

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

export default Account;

and in my routes/accounts.js I have this:

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

At this stage I'm simply trying to get a list of accounts from the fixtures displayed on screen. The route works nicely and if I put in static data (like the index route) then all works fine. However, with the code above, I run into trouble

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

Where am I going wrong?

4

3 回答 3

7

您的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
});

我希望它有帮助

于 2013-09-11T13:41:49.717 回答
2

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

当你打电话时:

store.find('account');

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

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

于 2014-01-06T04:50:56.903 回答
1

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

但是我可以通过导入我的 ApplicationAdapter 来解决这个问题,并使用它来定义我的商店:

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

var ApplicationAdapter = DS.FixtureAdapter.extend();

export default ApplicationAdapter;

应用/商店/application.js:

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

export default Store;

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

于 2013-09-29T04:59:50.630 回答