| Current Path : /home/kamilrogam/komp/2018/180513/DARWIN/EDU/vue/todo/ |
| Current File : /home/kamilrogam/komp/2018/180513/DARWIN/EDU/vue/todo/index.js |
class TodoItem {
constructor(id, text) {
this.id = id;
this.text = text;
}
}
Vue.component('todo-item', {
props: ['todo'],
template: '<span>{{todo.text}}</span>'
});
const app = new Vue({
el: '#app',
data: {
introText: 'Enter new item:',
instruction: 'productivity is the best',
itemsList: [
{
id: 0,
text: 'Pork'
},
{
id: 9,
text: 'Vegetables'
},
{
id: 1,
text: 'Cheese'
},
{
id: 2,
text: 'Whatever else humans are supposed to eat'
}
],
text: '',
btnTextAdd: 'add',
btnTextRemove: 'remove',
counter: 0,
showError: false
},
methods: {
add: function (text) {
if (text.length > 0) {
const index = document.querySelectorAll('#app li').length;
this.itemsList.push(new TodoItem(index + 1, text));
this.text = '';
} else {
this.error();
}
},
remove: function (index) {
this.itemsList.splice(index, 1);
},
update: function () {
this.counter = this.itemsList.length;
},
error: function () {
this.showError = true;
setTimeout(() => {
this.showError = false;
this.text = '';
}, 2000);
}
},
created: function () {
this.update()
},
watch: {
itemsList: function () {
this.update()
}
}
});
//var app6 = new Vue({
// el: '#app-6',
// data: {
// message: 'pisać w vue'
// }
//})
//
//Vue.component('1todo-item', {
// props: ['todo'],
// template: '<li>{{ todo.text }}</li>'
//})
//
//var app7 = new Vue({
// el: '#app-7',
// data: {
// groceryList: [
// {
// id: 0,
// text: 'Pork'
// },
// {
// id: 9,
// text: 'Vegetables'
// },
// {
// id: 1,
// text: 'Cheese'
// },
// {
// id: 2,
// text: 'Whatever else humans are supposed to eat'
// }
// ],
// listUnSorted: [0,9,1,3,5],
// listSorted: []
// },
// computed:{
// sorted: function(){
// return this.listUnSorted.sort();
// }
// }
//})
//
//var watchExampleVM = new Vue({
// el: '#watch-example',
// data: {
// question: '',
// answer: 'I cannot give you an answer until you ask a question!'
// },
// watch: {
// // whenever question changes, this function will run
// question: function (newQuestion, oldQuestion) {
// this.answer = 'Waiting for you to stop typing...'
// this.debouncedGetAnswer()
// }
// },
// created: function () {
// // _.debounce is a function provided by lodash to limit how
// // often a particularly expensive operation can be run.
// // In this case, we want to limit how often we access
// // yesno.wtf/api, waiting until the user has completely
// // finished typing before making the ajax request. To learn
// // more about the _.debounce function (and its cousin
// // _.throttle), visit: https://lodash.com/docs#debounce
// this.debouncedGetAnswer = _.debounce(this.getAnswer, 500)
// },
// methods: {
// getAnswer: function () {
// if (this.question.indexOf('?') === -1) {
// this.answer = 'Questions usually contain a question mark. ;-)'
// return
// }
// this.answer = 'Thinking...'
// var vm = this
// axios.get('https://yesno.wtf/api')
// .then(function (response) {
// vm.answer = _.capitalize(response.data.answer)
// })
// .catch(function (error) {
// vm.answer = 'Error! Could not reach the API. ' + error
// })
// }
// }
//})