From b657f26b2eb6a580171bb634795916990a5f1862 Mon Sep 17 00:00:00 2001 From: basejump Date: Mon, 8 May 2017 20:13:27 -0500 Subject: [PATCH] Phase 6 - Use v-model axios rest wrapper. Add error checking 1. `npm install v-model --save` or modify package.json per example and run npm install 2. added todoModel.js to replace the axios based todoRestApi from phase 5 3. modified main.js to use todoModel and the [vmodel](https://github.com/laoshu133/v-model) Model based methods 4. Add some error checking with `catch` and a div in index.html to diplay errors 5. Modify the Todo domain in grails so we can simulate an error by creaating an item with 'xxx' --- .../domain/grails/server/Todo.groovy | 2 +- vue-app/.eslintrc.js | 2 +- vue-app/index.html | 1 + vue-app/package.json | 5 ++- vue-app/src/main.js | 41 +++++++++++-------- vue-app/src/todoModel.js | 38 +++++++++++++++++ 6 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 vue-app/src/todoModel.js diff --git a/grails-server/grails-app/domain/grails/server/Todo.groovy b/grails-server/grails-app/domain/grails/server/Todo.groovy index 0f68e26..ab52500 100644 --- a/grails-server/grails-app/domain/grails/server/Todo.groovy +++ b/grails-server/grails-app/domain/grails/server/Todo.groovy @@ -10,7 +10,7 @@ class Todo { Boolean archived = false static constraints = { - title nullable: false + title nullable: false, notEqual: "xxx" completed nullable: false archived nullable: false } diff --git a/vue-app/.eslintrc.js b/vue-app/.eslintrc.js index 42f854f..d08f566 100644 --- a/vue-app/.eslintrc.js +++ b/vue-app/.eslintrc.js @@ -18,7 +18,7 @@ module.exports = { // add your custom rules here 'rules': { 'spaced-comment': 0, - 'quotes':0, "space-before-function-paren":0, + 'quotes':0, 'space-before-function-paren':0, 'semi':0, // allow paren-less arrow functions 'arrow-parens': 0, // allow async-await diff --git a/vue-app/index.html b/vue-app/index.html index 8784587..15dfc9a 100644 --- a/vue-app/index.html +++ b/vue-app/index.html @@ -12,6 +12,7 @@

todos

+

{{ errors.message }}

{ + todos.forEach(function (todo) { + todo.$update() + }) +} + +TodoModel.archiveCompleted = todos => { + todos.filter(function (todo) { + return todo.completed + }).forEach(function (todo) { + todo.$delete().then(response => { + todos.splice(todos.indexOf(todo), 1) + //console.log("deleted", todo) + }) + }) +} + +TodoModel.errors = { message: '' } //set to data. + +// Add a response interceptor for default errors from Grails rest +TodoModel.http.interceptors.response.use(function (response) { + TodoModel.errors.message = '' + return response +}, function (error) { + console.log("error.response ", error.response) + TodoModel.errors.message = error.response.data.message + return Promise.reject(error) +}) + +export default TodoModel