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 115<br />

1 var vm = new Vue({<br />

2 ...<br />

3 methods: {<br />

4 createStory: function(){<br />

5 var newStory={<br />

6 "plot": "",<br />

7 "upvotes": 0,<br />

8 "editing": true<br />

9 };<br />

10 this.stories.push(newStory);<br />

11 },<br />

12 }<br />

13 })<br />

1 Here's a list of all your stories.<br />

2 <br />

3 Add a new one?<br />

4 <br />

5 <br />

Info<br />

The push() method adds new items to the end of an array, and returns the new length. You<br />

can find more information about the push() method and its syntax Here⁵<br />

As soon as the new variable is set, we push it to our stories array. We named the new function<br />

createStory and we placed it in our Vue instance.<br />

Right bellow our list, we have added a button. When the button is clicked, createStory method gets<br />

invoked. Since the newStory.editing is set to true, the binded inputs for “plot” and “writer” along<br />

with the ‘Edit action buttons’ are being rendered instantly.<br />

Also, the new story object must be sent to the server in order to be stored in the database. We are<br />

going to perform a POST request inside a method called storeStory.<br />

⁵https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

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

Saved successfully!

Ooh no, something went wrong!