요약하면, Memento Factory에서 상태 관리를 추가 할 수 있습니다.
필요한 코드는 모두 아래에 있지만 내 블로그에는 배경이 조금 더 있습니다 (AngularJS Memento Factory).
function MementoFactory(){
return function() {
var memento = this;
// Private properties
var subjects = arguments; // We can track multiple objects or arrays
var stack = []; // Each call to "save" makes a copy of every subject on the stack
var currentIndex = 0; // The "current" position on the stack stack
// Begin by saving the current state
save();
// Public properties
memento.timestamp = null; // The timestamp for the current stack
// Public methods
memento.save = save;
memento.canUndo = canUndo;
memento.undo = undo;
memento.canRedo = canRedo;
memento.redo = redo;
function save() {
var snapshot = {
timestamp: Date.now(), // The save time
subjects: [], // Contains each of the subjects
};
for (var a = 0, al = subjects.length; a < al; a++) {
snapshot.subjects.push(angular.copy(subjects[a]));
}
if (stack[currentIndex] && angular.equals(stack[currentIndex].subjects, snapshot.subjects)) {
return; // Do nothing if the new snapshot is the same as the current snapshot
}
if (canRedo()) {
stack = stack.slice(0, currentIndex + 1); // Since we can "redo" we must overwrite that timeline (consider Back To The Future: Part II)
}
memento.timestamp = snapshot.timestamp; // Store the time
stack.push(snapshot);
currentIndex = stack.length - 1;
};
function canUndo() {
return currentIndex > 0;
};
function undo() {
if (canUndo()) {
restoreSnapshot(-1);
}
};
function canRedo() {
return currentIndex < stack.length - 1;
};
function redo() {
if (canRedo()) {
restoreSnapshot(+1);
}
};
function restoreSnapshot(indexDiff) {
currentIndex += indexDiff;
var snapshot = stack[currentIndex];
memento.timestamp = snapshot.timestamp; // Update the timestamp
for (var s = 0, sl = snapshot.subjects.length; s < sl; s++) {
if (snapshot.subjects[s] !== subjects[s]) {
angular.copy(snapshot.subjects[s], subjects[s]);
}
}
};
};
};
angular
.module('app')
.factory('Memento', MementoFactory);
당신이
ctrl.user = { name: 'David King', location: 'England' };
ctrl.tags = [ 'AngularJS', 'Angular', 'Firebase' ];
// Create a new Memento object
var memento = new Memento(ctrl.user, ctrl.tags);
// Expose the undo and redo methods
ctrl.canUndo = memento.canUndo;
ctrl.redo = memento.redo;
ctrl.canRedo = memento.canRedo;
ctrl.undo = memento.undo;
추가를 추적 취소하고보기에 버튼을 다시 할 비 원시적 변수를 전달, 새로운 메멘토 (...) 객체를 생성
<button
type="button"
ng-click="ctrl.undo()"
ng-disabled="!ctrl.canUndo">Undo</button>
<button
type="button"
ng-click="ctrl.redo()"
ng-disabled="!ctrl.canRedo">Redo</button>
적절한 경우 Memento 객체를 저장하십시오.
<input
type="text"
ng-model="ctrl.user.name"
ng-change="ctrl.save()">
<input
type="text"
ng-model="ctrl.user.location"
ng-change="ctrl.save()">
... 그게 전부 야!
Cronicle의 작동 방식을 간략히 살펴보면 변경 사항을 살펴 보는 것처럼 보입니다. 각도가 가장 높은 성능을 필요로하는 경우 (각도 작업으로 인해 코드가 여러 번 호출되는 경우가 많습니다). 따라서이 접근법에서 벗어나 자신이 소유 한 사람이 될 수 있습니다. 한 번 큰 목록의 필터에 문제가 발생하여 데이터 집합의 변경 (시계가 아닌 코드에 의해 트리거되는)에 필요한 경우에만 필터링을 수행하여 성능이 향상되는 것을 발견했습니다. – Tobi