【发布时间】: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