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

The fetchStories method<br />

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

2 el: '#v-app',<br />

3 data : {<br />

4 stories: [],<br />

5 },<br />

6<br />

7 ready : function(){<br />

8 this.fetchStories()<br />

9 },<br />

10 methods: {<br />

11 createStory: function(){<br />

12 var newStory={<br />

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

14 "upvotes": 0,<br />

15 "editing": true<br />

16<br />

17 };<br />

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

19 },<br />

20 fetchStories: function () {<br />

21 this.$http.get('/api/stories')<br />

22 .then(function (response) {<br />

23 var storiesReady = response.data.map(function(story){<br />

24 story.editing = false;<br />

25 return story<br />

26 })<br />

27 this.$set('stories', storiesReady)<br />

28 });<br />

29 },<br />

30 }<br />

31 });<br />

In our situation, we’ll call fetchStories() inside the success callback of the POST request.

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

Saved successfully!

Ooh no, something went wrong!