We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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,也可以单独使用单独使用
/* * @Author: grove.liqihan * @Date: 2017-10-10 10:08:14 * @Desc: 记录下操作历史 */ import _ from 'lodash'; // var state = { // data: { // a: 1 // } // } // let historyRemember = new HistoryRemember(state, () => { // }) // historyRemember.push({ // selector: 'data.a', // value: 3 // }) // console.log(state); // { data: { a: 3 } } // historyRemember.back(); // console.log(state); // { data: { a: 1 } } // historyRemember.forward(); // console.log(state); // { data: { a: 3 } } class HistoryRemember { constructor(state, onchange) { this.state = state; this.history = [null]; this.index = 0; this.onchange = onchange || function () {} } get length () { return this.history.length; } push(changes) { if (!Array.isArray(changes)) { changes = [changes]; } changes.forEach((change) => { change.oldValue = _.get(this.state, change.selector) || null; _.set(this.state, change.selector, _.cloneDeep(change.value)); }); this.index += 1; this.history.length = this.index; this.history.push(_.cloneDeep(changes)); this.onchange(); return true; } // 返回 back () { if (this.index === 0) { return false; } let changes = this.history[this.index]; changes.forEach(change => { _.set(this.state, change.selector, _.cloneDeep(change.oldValue)) }) this.index -=1; this.onchange(); return true; } forward () { if (this.index === this.history.length - 1) { return false; } var changes = this.history[this.index + 1]; changes.forEach(change => { if (change.value == null) { _.unset(this.state, change.selector) } else { _.set(this.state, change.selector, _.cloneDeep(change.value)) } }) this.index += 1; this.onchange(); return true; } clear () { this.history.length = 1 this.index = 0 this.onchange() return true } } export default HistoryRemember
The text was updated successfully, but these errors were encountered:
No branches or pull requests
###模拟用户的行为操作,不改变浏览器历史的情况下,实现了用户行为的记录,项目中可以结合vuex,也可以单独使用单独使用
The text was updated successfully, but these errors were encountered: