1

我正在使用本教程学习 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)
});

我的海市蜃楼配置如下

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 路线,我返回模型如下

路线/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}}

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

Ember 检查器输出

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

谢谢您的帮助。所有代码片段均来自教程。

4

1 回答 1

3

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

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