25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Saved page<br />

We will now add the saved page to <strong>Vue</strong>bnb. Let's begin by creating the<br />

component file: $ touch resources/assets/components/SavedPage.vue<br />

Next, we'll create a new route with this component at the path /saved.<br />

resources/assets/<strong>js</strong>/router.<strong>js</strong>: ... import SavedPage from<br />

'../components/SavedPage.vue'; let router = new <strong>Vue</strong>Router({ ... routes: [ ... {<br />

path: '/saved', component: SavedPage, name: 'saved' } ] });<br />

Let's also add some server-side routes to the <strong>Laravel</strong> project. As discussed<br />

above, the saved page uses exactly the same data as the home page. This means<br />

that we can just call the same controller methods used for the home page.<br />

routes/web.php: Route::get('/saved', 'ListingController@get_home_web');<br />

routes/api.php: Route::get('/saved', 'ListingController@get_home_api');<br />

Now we will define the SavedPage component. Beginning with the script tag, we<br />

will import the ListingSummary component we created back in Chapter 6, Composing<br />

Widgets with <strong>Vue</strong>.<strong>js</strong> Components. We'll also create a computed property, listings,<br />

that will return the listing summaries from the store, filtered by whether or not<br />

they're saved.<br />

resources/assets/components/SavedPage.vue: import<br />

ListingSummary from './ListingSummary.vue'; export default { computed: {<br />

listings() { return this.$store.state.listing_summaries.filter( item =><br />

this.$store.state.saved.indexOf(item.id) > -1 ); } }, components: {<br />

ListingSummary } } <br />

Next, we will add to the template tag of SavedPage. The main content includes a<br />

check for the length of the array returned by the listings computed property. If it<br />

is 0, no items have been saved yet. In this case, we display a message to inform<br />

the user. If there are listings saved, however, we'll iterate through them <strong>and</strong><br />

display them with the ListingSummary component.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!