Ember、JSONAPI适配器、JSONAPISerializer、findAll不可用

Ember, JSONAPIAdapter, JSONAPISerializer, findAll not available

本文关键字:findAll JSONAPISerializer JSONAPI 适配器 Ember      更新时间:2023-09-26

我正在构建一个Ember应用程序"Ember-cli":"2.4.3",坐在Laravel/Lumen上,似乎无法正确连接电线。我也试图让API服务器JSON-API兼容,所以如果有问题,我可以修改语法。

如果我删除导出默认DS.JSONAPISERIALIZER,我会得到ember.debug.js:32116 TypeError: typeClass.eachTransformedAttribute is not a function

有了它,我通常会得到Assertion Failed: You tried to load all records but your adapter does not implement findAll

如果我从路由中调用getJSON(...),而不是为数据调用存储,那么它工作得很好,并按预期显示在视图中。

我尝试过其他适配器,但我认为为了符合JSON-API,我需要使用JSONAPIADAPTER。任何帮助都会很棒。

application.js

import DS from "ember-data";
export default DS.JSONAPIAdapter.extend({
 namespace: 'v1',
 host: 'http://edu-api.app:8000',
});
export default DS.JSONAPISerializer.extend({
 //in preparation of underscores in returned data
 //   keyForAttribute: function(attr) {
 //     return Ember.String.underscore(attr);
 //   },
 //   keyForRelationship: function(attr) {
 //     return Ember.String.underscore(attr);
 //   }
});

skill.js

import DS from 'ember-data';
var App = window.App = Ember.Application.extend();
var attr = DS.attr;
App.Skill = DS.Model.extend({
  name: attr("string"),
  desc: attr("string")
});

index.js

export default Ember.Route.extend({
  model() {
    //return this.store.findAll('skill');  //<- Assertion Failed: You tried to load all records but your adapter does not implement `findAll`
    this.get('store').findAll('skill');  //<- Assertion Failed: You tried to load all records but your adapter does not implement `findAll`
    //return Ember.$.getJSON('http://edu-api.app:8000/v1/skills'); //<- works, and properly displays data to view
  }
});

我认为您主要在理解ember-cli方面存在问题。

首先,不要将适配器和序列化程序放在同一个文件中。也许可以使用生成器来获得像ember generate serializer application这样的默认文件。

应用程序序列化程序转到app/serializers/application.js,适配器转到app/adapters/application.js

接下来这一行看起来真的非常错误:

var App = window.App = Ember.Application.extend();

这将创建一个新的应用程序,但您应该在app/app.js中只执行一次。接下来,您将使用全局导出,这是您在ember-cli应用程序中不应该做的事情。

要指定您的型号,您需要在models/skill.js下找到您的文件。在那里,您不会像App.Skill = DS.Model.extend({那样将新模型附加到全局导出的App,而是像export default DS.Model.extend({那样将其导出为默认导出。

如果您的index.js位于routes/下方,则它看起来是正确的。

我强烈建议您阅读更多关于余烬解析器和余烬依赖注入框架的内容,它们为您带来了所有这些魔力。还可以使用生成器来获取文件,它可以帮助您正确放置文件。