[英]Ember.js - Model todo not found when using ember-cli-mirage for a fake model
I am learning ember.js with this tutorial When using ember-cli-mirage to create a fake model for todos like this 我正在学习使用本教程的 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)
});
my mirage config looks like following 我的海市蜃楼配置如下所示
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');
}
My confusion is mostly with models. 我的困惑主要是模特。 The name of model is 'todos' and ember somehow changes it to 'todo' when dealing with single record.
模型的名称是'todos',并且在处理单个记录时,ember以某种方式将其更改为“todo”。
From my todos route, I return the model as follows 从我的待办事项路线,我返回模型如下
routes/todos.js 路线/ todos.js
model() {
return this.store.findAll('todos');
}
And then I don't understand the above code - tutorial said it should be this.store.findAll(**'todo'**);
然后我不明白上面的代码 - 教程说它应该是
this.store.findAll(**'todo'**);
But that does not return any data to ember console. 但这并没有将任何数据返回到ember控制台。 I changed it to 'todos' and I see some output.
我把它改成'todos' ,我看到了一些输出。 (output at the end)
(最后输出)
In routes/todos/index.js -- We return modelFor('todos') 在routes / todos / index.js中 - 我们返回modelFor('todos')
model(){
return this.modelFor('todos');
}
And in the template -- todos/index.hbs 并在模板中 - todos / index.hbs
<ul id="todo-list">
{{#each model as |todo| }}
{{todo-item todo=todo}}
{{/each}}
</ul>
I understand that index gets this model from todos.hbs's {{outlet}} todos.hbs looks like below 据我所知,索引从todos.hbs获取此模型{{outlet}} todos.hbs如下所示
<input type="text" id="new-todo" placeholder="What needs to be done?" />
{{#todo-list todos=model}}
{{outlet}}
{{/todo-list}}
When I run this application, I get the following error. 当我运行此应用程序时,我收到以下错误。
In the output, I get the data from the get request on / --> this is todos route But I am not getting the access to todos in the todos/index route. 在输出中,我从/ - >获取get请求中的数据这是todos路由但是我没有在todos / index路由中访问todos。
Thanks for the help. 谢谢您的帮助。 All code snippets are from the tutorial.
所有代码片段均来自本教程。
Check the following (things I noticed): 检查以下(我注意到的事情):
findAll('todo')
! findAll('todo')
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.