私のアプリ ワークフローは、API バックエンドからデータを取得し、LokiJS を使用してクライアント ストレージに書き込みます。クライアント コレクションには何千ものデータがあります。アプリの状態管理には Vuex を使用しています。
そして、ここに問題があります。Vuex の状態に変更をコミットすると、LokiJS ストレージのデータも変更されます。どうすればこれを回避できますか?
LS (lokijs ストレージ) からユーザーを取得するための Vuex アクションがあります。
getStudentByClass({ state, commit }) {
const stds = students.collection.find({ class_id: state.classId });
commit('setStudents', stds);
}
selectStudent(state, index) {
const student = state.students[index];
Vue.set(student, 'selected', !student.selected);
}
および Vuex 突然変異
setStudents(state, students) {
state.students = students
}
上記のように; 関数を使用しselectStudent
て、学生が選択した Vuex ストアの属性を変更しますが、LS でも変更されます。
元の LS データは次のようになります
$loki: 63
class_id: 5
color: "green"
id: 248
meta: Object
name: "John Doe"
point: 100
teacher: 0
updated_at: "2017-01-24 10:00:34"
username: "johdoe"
LS データが変更されました
$loki: (...)
class_id: (...)
color: (...)
id: (...)
meta: (...)
name: (...)
point: (...)
selected: true <--------------- here
teacher: (...)
updated_at: (...)
username: (...)
__ob__: Observer
get $loki: function reactiveGetter()
set $loki: function reactiveSetter(newVal)
// skipped