Skip to content

Commit 2cc1c5c

Browse files
committed
Phase 6 - Use v-model - CLEAN UP and refactor
1 parent bf5052d commit 2cc1c5c

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

vue-app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>todos</h1>
2828
:key="todo.id"
2929
:class="{ completed: todo.completed, editing: todo == editedTodo }">
3030
<div class="view">
31-
<input class="toggle" type="checkbox" :checked="todo.completed" @click="setCompleted(todo)" >
31+
<input class="toggle" type="checkbox" :checked="todo.completed" @click="todo.$toggleCompleted()" >
3232
<label @dblclick="editTodo(todo)">{{ todo.title }}</label>
3333
<button class="destroy" @click="removeTodo(todo)"></button>
3434
</div>

vue-app/src/main.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ var app = new Vue({
7777
// note there's no DOM manipulation here at all.
7878
methods: {
7979
addTodo: function () {
80-
let self = this
81-
8280
var value = this.newTodo && this.newTodo.trim()
8381
if (!value) {
8482
return
@@ -88,15 +86,16 @@ var app = new Vue({
8886
title: value
8987
})
9088

91-
newTodoObj.$promise.then(function (response) {
92-
self.todos.push(newTodoObj)
89+
newTodoObj.$promise.then(resp => {
90+
this.todos.push(newTodoObj)
9391
console.log("addTodo ", newTodoObj)
94-
self.newTodo = ''
92+
this.newTodo = ''
9593
})
9694
},
9795
removeTodo: function (todo) {
98-
todo.$delete()
99-
this.todos.splice(this.todos.indexOf(todo), 1)
96+
todo.$delete().then(resp => {
97+
this.todos.splice(this.todos.indexOf(todo), 1)
98+
})
10099
},
101100

102101
editTodo: function (todo) {
@@ -113,9 +112,8 @@ var app = new Vue({
113112
if (!todo.title) {
114113
this.removeTodo(todo)
115114
} else {
116-
let self = this
117-
todo.$update().catch(function (ex) {
118-
self.editedTodo = todo //reset it back so input doesn't go away
115+
todo.$update().catch(ex => {
116+
this.editedTodo = todo //reset it back so input doesn't go away
119117
})
120118
}
121119
},
@@ -127,11 +125,6 @@ var app = new Vue({
127125

128126
removeCompleted: function () {
129127
TodoModel.archiveCompleted(this.todos)
130-
},
131-
132-
setCompleted: function(todo) {
133-
todo.completed = !todo.completed
134-
todo.$update()
135128
}
136129
},
137130

@@ -149,7 +142,6 @@ var app = new Vue({
149142
created: function() {
150143
console.log("Ready")
151144
this.todos = TodoModel.query()
152-
//TodoModel.list(this.todos)
153145
}
154146
})
155147

vue-app/src/todoModel.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
1+
import Vue from 'vue'
12
import Model from 'v-model'
23
import Promise from 'bluebird'
34

45
// set baseURL and install plugin
56
Model.http.defaults.baseURL = 'http://localhost:8080/api/';
7+
Vue.use(Model)
68

79
const TodoModel = Model.extend('/todo/:id')
810

9-
TodoModel.updateAll = todos => {
10-
todos.forEach(function (todo) {
11-
todo.$update()
12-
})
13-
}
11+
Object.assign(TodoModel, {
12+
errors: {
13+
message: ''
14+
},
1415

15-
TodoModel.archiveCompleted = todos => {
16-
todos.filter(function (todo) {
17-
return todo.completed
18-
}).forEach(function (todo) {
19-
todo.$delete().then(response => {
20-
todos.splice(todos.indexOf(todo), 1)
21-
//console.log("deleted", todo)
16+
updateAll(todos) {
17+
todos.forEach(function (todo) {
18+
todo.$update()
2219
})
23-
})
24-
}
20+
},
21+
22+
archiveCompleted(todos) {
23+
todos.filter(function (todo) {
24+
return todo.completed
25+
}).forEach(function (todo) {
26+
todo.$delete().then(response => {
27+
todos.splice(todos.indexOf(todo), 1)
28+
//console.log("deleted", todo)
29+
})
30+
})
31+
}
32+
33+
});
2534

26-
TodoModel.errors = { message: '' } //set to data.
35+
//toggles the completed and saves to server
36+
TodoModel.prototype.$toggleCompleted = function() {
37+
console.log("$toggleCompleted", this)
38+
this.completed = !this.completed
39+
this.$update()
40+
}
2741

28-
// Add a response interceptor for default errors from Grails rest
42+
// Add a default response interceptor for default errors from Grails rest
43+
//see https://github.com/mzabriskie/axios#handling-errors
2944
TodoModel.http.interceptors.response.use(function (response) {
3045
TodoModel.errors.message = ''
3146
return response
3247
}, function (error) {
33-
console.log("error.response ", error.response)
34-
TodoModel.errors.message = error.response.data.message
48+
if (error.response) {
49+
// The request was made and the server responded with a status code
50+
// that falls out of the range of 2xx
51+
TodoModel.errors.message = error.response.data.message
52+
console.log('response Error ', error.response)
53+
} else if (error.request) {
54+
// The request was made but no response was received
55+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
56+
// http.ClientRequest in node.js
57+
console.log("request error ", error.request);
58+
TodoModel.errors.message = error.request
59+
} else {
60+
// Something happened in setting up the request that triggered an Error
61+
console.log('Error ', error.message);
62+
TodoModel.errors.message = error.message
63+
}
3564
return Promise.reject(error)
3665
})
3766

0 commit comments

Comments
 (0)