0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Merge pull request #2090 from sveltejs/gh-2078

fix two examples
This commit is contained in:
Rich Harris 2019-02-17 13:51:34 -04:00 committed by GitHub
commit 5c70456123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 23 deletions

View File

@ -3,13 +3,22 @@
<script> <script>
export let people = []; export let people = [];
let filteredPeople;
let selected;
let prefix = ''; let prefix = '';
let first = ''; let first = '';
let last = ''; let last = '';
let i = 0; let i = 0;
$: filteredPeople = prefix
? people.filter(person => {
const name = `${person.last}, ${person.first}`;
return name.toLowerCase().startsWith(prefix.toLowerCase());
})
: people;
$: selected = filteredPeople[i];
$: reset_inputs(selected);
function create() { function create() {
people = people.concat({ first, last }); people = people.concat({ first, last });
i = people.length - 1; i = people.length - 1;
@ -27,18 +36,8 @@
i = Math.min(i, people.length - 1); i = Math.min(i, people.length - 1);
} }
$: filteredPeople = prefix function reset_inputs(person) {
? people.filter(person => { ({ first, last } = person);
const name = `${person.last}, ${person.first}`;
return name.toLowerCase().startsWith(prefix.toLowerCase());
})
: people;
$: selected = filteredPeople[i];
$: if (selected) {
first = selected.first;
last = selected.last;
} }
</script> </script>

View File

@ -1,26 +1,24 @@
<script> <script>
const tomorrow = new Date(Date.now() + 86400000); const tomorrow = new Date(Date.now() + 86400000);
const tomorrowAsString = [ let start = [
tomorrow.getFullYear(), tomorrow.getFullYear(),
pad(tomorrow.getMonth() + 1, 2), pad(tomorrow.getMonth() + 1, 2),
pad(tomorrow.getDate(), 2) pad(tomorrow.getDate(), 2)
].join('-'); ].join('-');
let start = tomorrowAsString; let end = start;
let end = tomorrowAsString;
let isReturn = false; let isReturn = false;
const startDate = () => convertToDate(start); $: startDate = convertToDate(start);
const endDate = () => convertToDate(end); $: endDate = convertToDate(end);
function bookFlight() { function bookFlight() {
const type = isReturn ? 'return' : 'one-way'; const type = isReturn ? 'return' : 'one-way';
let message = `You have booked a ${type} flight, leaving ${startDate().toDateString()}`; let message = `You have booked a ${type} flight, leaving ${startDate.toDateString()}`;
if (type === 'return') { if (type === 'return') {
message += ` and returning ${endDate().toDateString()}`; message += ` and returning ${endDate.toDateString()}`;
} }
alert(message); alert(message);
@ -57,5 +55,5 @@
<button <button
on:click={bookFlight} on:click={bookFlight}
disabled="{isReturn && (startDate() >= endDate())}" disabled="{isReturn && (startDate >= endDate)}"
>book</button> >book</button>