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.

Components 77<br />

16 <br />

17 <br />

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

2 template: "#story-template",<br />

3 props: ['story'],<br />

4 methods:{<br />

5 upvote: function(){<br />

6 this.story.upvotes += 1;<br />

7 this.story.voted = true;<br />

8 },<br />

9 setFavorite: function(){<br />

10 this.favorite = this.story;<br />

11 },<br />

12 },<br />

13 computed:{<br />

14 isFavorite: function(){<br />

15 return this.story == this.favorite;<br />

16 },<br />

17 }<br />

18 });<br />

19<br />

20 new Vue({<br />

21 el: '#app',<br />

22 data: {<br />

23 stories: [<br />

24 ...<br />

25 ],<br />

26 favorite: {}<br />

27 }<br />

28 })<br />

If you try to run the above code, you will notice that it does not work as it should be. Whenever you<br />

try to favorite a story, the variable favorite inside $data remains null and we get none response.<br />

It seems that our story component is unable to update favorite object, so we are going to pass it<br />

on each story and add favorite to component’s properties.

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

Saved successfully!

Ooh no, something went wrong!