Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vuex #2

Merged
merged 4 commits into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
初步vuex重构
  • Loading branch information
testudy committed Aug 5, 2018
commit 9cc70c5b2a9c0257dfb4861ad2bd8345a38b066a
103 changes: 27 additions & 76 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import CommonForm from './components/CommonForm.vue';
import TodoListItem from './components/TodoListItem.vue';

Expand All @@ -54,85 +55,35 @@ export default {
CommonForm,
TodoListItem,
},
data() {
return {
todos: [],
toggleCheckAllState: false,
};
},
watch: {
todos() {
let checkAllState = true;
if (this.todos.length === 0) {
checkAllState = false;
} else {
this.todos.forEach((item) => {
computed: {
...mapState([
'todos',
]),
...mapState({
toggleCheckAllState: (state) => {
if (state.todos.length === 0) {
return false;
}

for (let i = 0, len = state.todos.length; i < len; i += 1) {
const item = state.todos[i];
if (!item.checked) {
checkAllState = false;
return false;
}
});
}
this.toggleCheckAllState = checkAllState;
},

/*
存在循环watch,如何解决呢?问强哥
toggleCheckAllState: function (checked) {
console.log(arguments);
this.todos = this.todos.map((item) => {
return Object.assign({}, item, {checked: checked});
});
},
*/
},
methods: {
/*
* 利用random随机数生成伪GUID
* https://zh.wikipedia.org/wiki/全局唯一标识符
* https://baike.baidu.com/view/185358.htm
*/
guid() {
const raw = [
Math.random().toString(31).substr(2),
Math.random().toString(31).substr(2),
Math.random().toString(31).substr(2),
].join('').substr(0, 32);
return raw.replace(/(\S{8})(\S{4})(\S{4})(\S{4})(\S{12})/, '$1-$2-$3-$4-$5');
},

create(modal) {
console.log(modal);
const newModal = Object.assign(modal, { id: this.guid() });
this.todos.push(newModal);
},

update(modal) {
const index = this.todos.findIndex(item => item.id === modal.id);
this.todos.splice(index, 1, modal);
},

remove(id) {
const index = this.todos.findIndex(item => item.id === id);
this.todos.splice(index, 1);
},

toggleCheckAll(checked) {
this.todos = this.todos.map(item => Object.assign({}, item, { checked }));
},

checkAll() {
this.todos = this.todos.map(item => Object.assign({}, item, { checked: true }));
},

toggle() {
this.todos = this.todos.map(item => Object.assign({}, item, { checked: !item.checked }));
},

clean() {
this.todos = this.todos.filter(item => !item.checked);
},

}
return true;
},
}),
},
methods: mapMutations([
'create',
'update',
'remove',
'toggleCheckAll',
'checkAll',
'toggle',
'clean',
]),
};
</script>

Expand Down
38 changes: 37 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
import Vue from 'vue';
import Vuex from 'vuex';

import { guid } from './util';

Vue.use(Vuex);

export default new Vuex.Store({
state: {
strict: process.env.NODE_ENV !== 'production',

state: {
todos: [],
},

mutations: {
create(state, payload) {
const model = Object.assign({}, payload, { id: guid() });
state.todos.push(model);
},

update(state, payload) {
const index = state.todos.findIndex(item => item.id === payload.id);
const model = Object.assign({}, state.todos[index], payload);
state.todos.splice(index, 1, model);
},

remove(state, id) {
const index = state.todos.findIndex(item => item.id === id);
state.todos.splice(index, 1);
},

toggleCheckAll(state, checked) {
state.todos = state.todos.map(item => Object.assign({}, item, { checked }));
},

checkAll(state) {
state.todos = state.todos.map(item => Object.assign({}, item, { checked: true }));
},

toggle(state) {
state.todos = state.todos.map(item => Object.assign({}, item, { checked: !item.checked }));
},

clean(state) {
state.todos = state.todos.filter(item => !item.checked);
},
},

actions: {

},
Expand Down
17 changes: 17 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 利用random随机数生成伪GUID
* https://zh.wikipedia.org/wiki/全局唯一标识符
* https://baike.baidu.com/view/185358.htm
*/
export function guid() {
const raw = [
Math.random().toString(31).substr(2),
Math.random().toString(31).substr(2),
Math.random().toString(31).substr(2),
].join('').substr(0, 32);
return raw.replace(/(\S{8})(\S{4})(\S{4})(\S{4})(\S{12})/, '$1-$2-$3-$4-$5');
}

export default {
guid,
};