【问题标题】:Ember.js - Model todo not found when using ember-cli-mirage for a fake modelEmber.js - 将 ember-cli-mirage 用于假模型时未找到模型
【发布时间】:2015-11-10 19:09:58
【问题描述】:

我正在用这个tutorial 学习 ember.js 当使用 ember-cli-mirage 为这样的待办事项创建假模型时

import Mirage, {faker} from 'ember-cli-mirage';

export default Mirage.Factory.extend({
    title(i) { return 'Todo title ${i + 1}'; },
    complete: faker.list.random(true, false)
});

我的 Mirage 配置如下所示

export default function() {
    this.get('/todos', function(db, request) {
        return {
            data: db.todos.map(attrs => (
                {type: 'todos', id: attrs.id, attributes: attrs }
            ))
        };
    });
    this.post('/todos', function(db, request) {
        let attrs = JSON.parse(request.requestBody);
        let todo = db.todos.insert(attrs);
        return {
            data: {
                type: 'todos',
                id: todo.id,
                attributs: todo
            }
        };
    });
    this.patch('/todos/:id', function(db, request) {
        let attrs = JSON.parse(request.requestBody);
        let todo = db.todos.update(attrs.data.id, attrs.data.attributes);
        return {
            data: {
                type: "todos",
                id: todo.id,
                attributes: todo
            }
        };
    });
    this.del('/todos/:id');
}

我的困惑主要在于模型。模型的名称是“todos”,而 ember 在处理单个记录时会以某种方式将其更改为“todo”。

从我的 todos 路由中,我返回模型如下

routes/todos.js

  model() {
        return this.store.findAll('todos');
    }

然后我不明白上面的代码 - 教程说它应该是 this.store.findAll(**'todo'**); 但这不会将任何数据返回到 ember 控制台。我将其更改为 'todos' 并且我看到了一些输出。 (最后输出)

在 routes/todos/index.js -- 我们返回 modelFor('todos')

  model(){
    return this.modelFor('todos');
  }

在模板中——todos/index.hbs

<ul id="todo-list">
  {{#each model as |todo| }}
      {{todo-item todo=todo}}
  {{/each}}
</ul>

我了解 index 从 todos.hbs 的 {{outlet}} 获取此模型 todos.hbs 如下所示

<input type="text" id="new-todo" placeholder="What needs to be done?" />

{{#todo-list todos=model}}
  {{outlet}}
{{/todo-list}}

当我运行此应用程序时,我收到以下错误。

在输出中,我从 get 请求中获取数据 / --> 这是 todos 路由 但我无法访问 todos/index 路由中的 todos。

感谢您的帮助。所有代码 sn-ps 均来自教程。

【问题讨论】:

    标签: javascript ember.js ember-data handlebars.js ember-cli-mirage


    【解决方案1】:

    检查以下(我注意到的事情):

    1. 您的模型文件是否名为“todo.js”?应该是单数...
    2. 您的路线应该是单数findAll('todo')!
    3. 您在 mirage 配置中的发布路线有错字:“attributs: todo”应该是属性。
    4. 您正在返回 JSONAPI 格式的数据。您是否使用 JSONAPIAdapter 而不是 RestAdapter 作为您的应用程序适配器?

    【讨论】:

    • 问题是复数 todos.js 文件我不确定 JSONAPI。我会检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    相关资源
    最近更新 更多