Created
June 29, 2017 19:59
-
-
Save CodingItWrong/8cc043aa50778214a610089d891d6288 to your computer and use it in GitHub Desktop.
Vue.js One-Way Data Binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div id="app"> | |
| <ul> | |
| <li v-for="string in strings"> | |
| {{string}} | |
| </li> | |
| </ul> | |
| <ul> | |
| <li v-for="(string, index) in strings"> | |
| <togglable-text-input v-bind:string="string" v-bind:index="index" v-on:updateString="handleUpdateString" /> | |
| </li> | |
| </ul> | |
| </div> | |
| </template> | |
| <script> | |
| import TogglableTextInput from './TogglableTextInput.vue'; | |
| export default { | |
| name: 'app', | |
| components: { | |
| TogglableTextInput | |
| }, | |
| data () { | |
| return { | |
| strings: [ "foo", "bar", "baz" ] | |
| } | |
| }, | |
| methods: { | |
| handleUpdateString(index, newString) { | |
| console.log('App.handleUpdateString()', index, newString); | |
| let modifiedNewString = `${newString}!!!`; | |
| this.strings.splice(index, 1, modifiedNewString); | |
| } | |
| } | |
| } | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div> | |
| <div v-if="editing"> | |
| <input v-model="stringBeingEdited" /> | |
| <button v-on:click="handleSave">Save</button> | |
| </div> | |
| <div v-else> | |
| {{string}} | |
| <button v-on:click="handleEdit">Edit</button> | |
| </div> | |
| </div> | |
| </template> | |
| <script> | |
| export default { | |
| name: 'togglable-text-input', | |
| props: ['string', 'index'], | |
| data() { | |
| return { | |
| editing: false, | |
| stringBeingEdited: '', | |
| }; | |
| }, | |
| methods: { | |
| handleEdit() { | |
| this.stringBeingEdited = this.string; | |
| this.editing = true; | |
| }, | |
| handleSave() { | |
| this.editing = false; | |
| this.$emit('updateString', this.index, this.stringBeingEdited); | |
| } | |
| } | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment