08.02.2017 Views

vuejs

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

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

Working with real data 99<br />

To do so , we’ll perform a HTTP GET request using jQuery at first. Later on this chapter, we will<br />

migrate to vue-resource¹ to see the differences between the two of them.<br />

To make the AJAX call we are going to use $.get(), a jQuery function that loads data from the<br />

server using a HTTP GET request. Full documentation for $.get() can be found here².<br />

Info<br />

vue-resource is a plugin for Vue.js that provides services for making web requests and<br />

handle responses.<br />

The $.get() method’s syntax is<br />

1 $.get(<br />

2 url,<br />

3 success<br />

4 );<br />

which is actually a shorthand for<br />

1 $.ajax({<br />

2 url: url,<br />

3 success: success<br />

4 });<br />

So what we do now? We want to get the stories from the server using $.get('/api/stories')<br />

passing the appropriate URL and put the response data we get, inside the stories array.<br />

There is a common catch here, we have to make the call after the documented has finished<br />

rendering. Fortunately, there is a helper function called ready in Vue.js (similar to $( document<br />

).ready()) which triggers once the page Document Object Model (DOM) is ready.<br />

Lets see this in action.<br />

¹https://github.com/<strong>vuejs</strong>/vue-resource<br />

²https://api.jquery.com/jquery.get/

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

Saved successfully!

Ooh no, something went wrong!