Skip to content

Instantly share code, notes, and snippets.

@mysteriousHerb
Last active June 2, 2019 22:08
Show Gist options
  • Select an option

  • Save mysteriousHerb/c07993c118e69c0ee6e074801c9043b2 to your computer and use it in GitHub Desktop.

Select an option

Save mysteriousHerb/c07993c118e69c0ee6e074801c9043b2 to your computer and use it in GitHub Desktop.

Revisions

  1. Tianheng revised this gist Jun 2, 2019. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions todo_rest.vue
    Original file line number Diff line number Diff line change
    @@ -38,10 +38,6 @@ export default {
    this.read_todo();
    },
    computed: {
    // the id of the new todos is the last id + 1
    new_todo_id: function() {
    this.todos[this.todos.length - 1].id + 1;
    }
    },
    methods: {
    // all the methods will be replaced with REST API call later
    @@ -57,7 +53,7 @@ export default {
    delete_ = false
    ) {
    var data = {
    todo_args: { id: id, content: content, done: done, delete: delete_ }
    id: id, content: content, done: done, delete: delete_
    };
    console.log(data);
    this.axios
  2. Tianheng created this gist Jun 2, 2019.
    73 changes: 73 additions & 0 deletions todo_rest.vue
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <template>
    <div id="todos-vue">
    <div>
    <label>New task:</label>
    <!-- use v-model for 2-way binding, call method when user press enter @keyup.enter-->
    <input id="new_todo" v-model="new_todo" placeholder="edit me" @keyup.enter="add_todo()">
    <button @click="update_todo(id=-1, content=new_todo, done=false, delete_=false)">Add</button>
    </div>
    <div>
    <div v-for="(todo) in todos" :key="todo.id">
    <label>{{todo.id}}.</label>
    <!-- we also want to be able to directly update the existing todo, this is handled by v-model, but our database needs to be handled differently-->
    <input v-model="todo.content" :disabled="todo.done" @keyup.enter="update_todo(id=todo.id, content=todo.content, done=todo.done, delete_=false)">
    <!-- when click the checkbox, the input becomes disabled -->
    <!-- use @change rather than @click, the @click event happens too fast that new_val is not sent through -->
    <input type="checkbox" v-model="todo.done" @change="update_todo(id=todo.id, content=todo.content, done=todo.done, delete_=false)">
    <button @click="update_todo(id=todo.id, content='', done=true, delete_=true)">Delete</button>
    </div>
    <button @click="read_todo()">read todo</button>
    <button @click="update_todo()">update todo</button>
    </div>
    </div>
    </template>

    <script>
    import { setTimeout } from "timers";
    export default {
    name: "todo_comp",
    data: function() {
    return {
    new_todo: "",
    todos: [
    { id: 1, content: "", done: false},
    ],
    };
    },
    mounted: function() {
    this.read_todo();
    },
    computed: {
    // the id of the new todos is the last id + 1
    new_todo_id: function() {
    this.todos[this.todos.length - 1].id + 1;
    }
    },
    methods: {
    // all the methods will be replaced with REST API call later
    read_todo: function() {
    this.axios
    .get("http://localhost:5000/todo_db")
    .then(response => (this.todos = response.data));
    },
    update_todo: function(
    id = -1,
    content = "",
    done = false,
    delete_ = false
    ) {
    var data = {
    todo_args: { id: id, content: content, done: done, delete: delete_ }
    };
    console.log(data);
    this.axios
    .post("http://localhost:5000/todo_db", data)
    .then(() => this.read_todo());
    // add a delay to make sure the backend respond
    },
    print:function(data){
    console.log(data)
    }
    }
    };
    </script>