08.02.2017 Views

vuejs

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Integrating vue-resource 120<br />

1 Vue.component('story',{<br />

2 ...<br />

3 methods: {<br />

4 ...<br />

5 storeStory: function(story){<br />

6 this.$http.post('/api/stories/', story).then(function() {<br />

7 story.editing = false;<br />

8 vm.fetchStories();<br />

9 });<br />

10 },<br />

11 }<br />

12 ...<br />

13 })<br />

That’s it! We can now create and edit any story we want.<br />

10.3.3 Store & Update Unit<br />

A better way to fix the previous issue, is to update only the newly created story from the database,<br />

instead of fetching and overwriting all the stories. If you see the server response for the POST request<br />

you will see that it returns the created story along with its id.<br />

Server response after creating new story<br />

The only thing we have to do, is to update our story to match the server’s one. So, we will set the<br />

id of the response data, to story’s id attribute. We will do this inside the POST’s success callback.

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

Saved successfully!

Ooh no, something went wrong!