在我的余烬应用中,我正在显示一系列图书
BOOKS
Book Author
Gorgeous Metal Fish Cookbook Jefferey Gibson
我试图在点击作者姓名后显示作者的信息(在模态窗口中) 在我的书籍模板中,我有一个动作'showAuthor',在书籍路线中定义,但我得到未定义的数据......
<span {{action 'showAuthor' book}}>{{book.author.name}}</span>
**TEMPLATES**
<!-- app/templates/books.hbs -->
<h1>Books</h1>
<table class="table table-bordered table-striped">
<thead>
. . .
</thead>
<tbody>
{{#each model as |book|}}
<tr>
<td>
. . .
</td>
<td>
<span {{action 'showAuthor' book}}>{{book.author.name}}</span>
</td>
</tr>
{{/each}}
</tbody>
</table>
============
**MODELS**
// app/models/book.js
export default Model.extend({
. . .
author: belongsTo('author', {inverse: 'books', async: true}),
. . .
});
// app/models/author.js
export default Model.extend({
. . .
books: hasMany('book', {inverse: 'author', async: true}),
. . .
});
=========
**ROUTES**
app/routes/books.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('book');
},
actions: {
.. .
showBookAuthor(book){
alert('SHOW BOOK AUTHOR : ' + book.author.name);
},
. . .
}
});
app/routes/authors.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('author');
},
actions: {
.. .
}
});
更新1 mybooks模板中的操作已正确编码:
{{book.author.name}}
并且在书籍路线中的操作被正确写入:
showBookAuthor(book){
alert('SHOW BOOK AUTHOR : ' + book.author.nale);
},
更新2
如指南中所述“,在处理人际关系时,重要的是要记住他们会回复承诺......
我更新了我的行动:
showBookAuthor(book){
book.get('author').then((author) => {
// now we can work with the author
console.log('SHOW BOOK AUTHOR : ' + author.name);
});
},
控制台不显示作者;名称
SHOW BOOK AUTHOR : [object Object]
我的编码出了什么问题?感谢您的反馈
答案 0 :(得分:0)
在模板中,您要将book.author
传递给showBookAuthor
操作,但是在您预期的图书路线中定义的showBookAuthor
中。
您应该将操作更改为:
showBookAuthor(author){
alert('SHOW BOOK AUTHOR : ' + author.name);
}
答案 1 :(得分:0)
已解决:
我应该按照以下方式编写我的行动:
showBookAuthor(book){
book.get('author').then((author) => {
// now we can work with the author
console.log('SHOW BOOK AUTHOR : ' + author.get('name'));
});
},