如何在路由之间传递选定的值?
How to pass selected value between routes?
我对 EmberJS 很陌生。
这是我的模型:book.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
picture: DS.attr('string'),
buyer: DS.attr('string'),
bought: DS.attr('boolean', { defaultValue: false }),
createdAt: DS.attr('date', { defaultValue() { return new Date(); } })
});
我有以下路线:
Books.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('book');
}
});
和相应的books.hbs
中的一点
{{#each model as |book|}}
<div class="book-container col-xs-12 col-sm-6 col-md-4">
<div class="card">
<div class="wrapper">
<div class="details col-md-12">
<h3 class="product-title">Title {{book.title}}</h3>
<p class="product-description">Author:{{book.author}}</p>
{{#link-to 'books.give' }} Buy {{/link-to}}
</div>
</div>
</div>
</div>
{{/each}}
此代码 returns 我目前存储在应用程序中的所有书籍。
想法是从books.hbs中选择Book
object并显示在[=62=中]。
我想用这条线
{{#link-to 'books.give' }} Buy {{/link-to}}
把一本书从books.hbs传到give.hbs,但我不知道如何做到这一点,如果它是要走的路..
在 gift.hbs 中,我部分发明了一种非常低效的方法,即再次循环所有书籍,如果标题匹配则显示它...
{{#each model as |book|}}
{{#if (eq book.title "Harry Potter")}}
<h2><b>{{book.title}}</b></h2>
{{/if}}
{{/each}}
显然 "Harry Potter" 字符串是 hard-coded 但它可能是从 books.hbs 传递过来的,因为我的数据比较小。
这取决于你想如何表示URL。
如果是 books/give/bookId
=> 然后选择 dynamic segments
如果它 books/give?bookId=1
=> 然后去 query params 实现
在 books.give
路线中使用 findRecord 获取特定的书籍记录并使用它。
我对 EmberJS 很陌生。
这是我的模型:book.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
picture: DS.attr('string'),
buyer: DS.attr('string'),
bought: DS.attr('boolean', { defaultValue: false }),
createdAt: DS.attr('date', { defaultValue() { return new Date(); } })
});
我有以下路线: Books.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('book');
}
});
和相应的books.hbs
中的一点{{#each model as |book|}}
<div class="book-container col-xs-12 col-sm-6 col-md-4">
<div class="card">
<div class="wrapper">
<div class="details col-md-12">
<h3 class="product-title">Title {{book.title}}</h3>
<p class="product-description">Author:{{book.author}}</p>
{{#link-to 'books.give' }} Buy {{/link-to}}
</div>
</div>
</div>
</div>
{{/each}}
此代码 returns 我目前存储在应用程序中的所有书籍。
想法是从books.hbs中选择Book
object并显示在[=62=中]。
我想用这条线
{{#link-to 'books.give' }} Buy {{/link-to}}
把一本书从books.hbs传到give.hbs,但我不知道如何做到这一点,如果它是要走的路..
在 gift.hbs 中,我部分发明了一种非常低效的方法,即再次循环所有书籍,如果标题匹配则显示它...
{{#each model as |book|}}
{{#if (eq book.title "Harry Potter")}}
<h2><b>{{book.title}}</b></h2>
{{/if}}
{{/each}}
显然 "Harry Potter" 字符串是 hard-coded 但它可能是从 books.hbs 传递过来的,因为我的数据比较小。
这取决于你想如何表示URL。
如果是 books/give/bookId
=> 然后选择 dynamic segments
如果它 books/give?bookId=1
=> 然后去 query params 实现
在 books.give
路线中使用 findRecord 获取特定的书籍记录并使用它。