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.

Integrating vue-resource 110<br />

Request shortcuts<br />

1 this.$http.get(url, [data], [options]).then(successCallback, errorCallback);<br />

2 this.$http.post(url, [data], [options]).then(successCallback, errorCallback);<br />

3 this.$http.put(url, [data], [options]).then(successCallback, errorCallback);<br />

4 this.$http.patch(url, [data], [options]).then(successCallback, errorCallback);<br />

5 this.$http.delete(url, [data], [options]).then(successCallback, errorCallback);<br />

10.2 Migrating<br />

It is time to use vue-resource in our example. First of all, we have to include it. We will add this<br />

line to our HTML file.<br />

1 <br />

To fetch the stories we will make a GET request in the corresponding form.<br />

1 ready: function() {<br />

2 // GET request<br />

3 this.$http({url: '/api/stories', method: 'GET'}).then(function (response) {<br />

4 this.$set('stories', response.data)<br />

5 //Or we as we did before<br />

6 //vm.stories = response.data<br />

7 })<br />

8 }<br />

Our list of stories comes without any problems using the syntax above.<br />

Lets move on now with the DELETE and PATCH requests using the shortcut methods.<br />

PATCH request<br />

1 upvoteStory: function(story){<br />

2 story.upvotes++;<br />

3 this.$http.patch('/api/stories/'+story.id , story)<br />

4 },<br />

We have replaced the AJAX method with this one, in no time!

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

Saved successfully!

Ooh no, something went wrong!