[英]How to pass selected value between routes?
I am quite new to the EmberJS.我对 EmberJS 很陌生。
This is my model: book.js这是我的模型: 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(); } })
});
I have the following route: Books.js我有以下路线: Books.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.findAll('book');
}
});
and a bit from corresponding books.hbs和一些来自相应的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}}
This code returns all the books that I currently store in the app.此代码返回我当前存储在应用程序中的所有书籍。
The idea is to get selected Book
object from books.hbs and display it in give.hbs .这个想法是从books.hbs 中获取选定的Book
对象并将其显示在give.hbs 中。
I wanted to use this line我想用这条线
{{#link-to 'books.give' }} Buy {{/link-to}}
to pass single a single book from books.hbs to give.hbs , but I don't know how to do that and if its the way to go..将books.hbs 中的一本书传递给give.hbs ,但我不知道该怎么做,以及是否可行。
In gift.hbs I partially invented a very inefficient way of doing it by looping all the books again and if the title is a match then display it...在gift.hbs 中,我通过再次循环所有书籍部分发明了一种非常低效的方法,如果标题匹配,则显示它......
{{#each model as |book|}}
{{#if (eq book.title "Harry Potter")}}
<h2><b>{{book.title}}</b></h2>
{{/if}}
{{/each}}
Obviously the "Harry Potter" string is hard-coded but it could have been passed from books.hbs and that would do the trick since my data is relatively small.显然“哈利波特”字符串是硬编码的,但它可以从books.hbs传递,因为我的数据相对较小,所以可以解决问题。
It depends on how do you want to represent URL.这取决于您想如何表示 URL。
if it's books/give/bookId
=> then go for dynamic segments如果是books/give/bookId
=> 然后去动态段
if its books/give?bookId=1
=> then go for query params implementation如果它的books/give?bookId=1
=> 然后去查询参数实现
Inside books.give
route get the particular book record using findRecord and use it.在books.give
路由中使用findRecord获取特定的书籍记录并使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.