How to cancel/revert changes to an observable model (or replace model in array with untouched copy)

前端 未结 5 1962
萌比男神i
萌比男神i 2021-01-01 12:19

I have a viewModel with an observableArray of objects with observable variables.

My template shows the data with an edit button that hides the display elements and s

5条回答
  •  心在旅途
    2021-01-01 12:47

    You might consider using KO-UndoManager for this. Here's a sample code to register your viewmodel:

    viewModel.undoMgr = ko.undoManager(viewModel, {
      levels: 12,
      undoLabel: "Undo (#COUNT#)",
      redoLabel: "Redo"
    });
    

    You can then add undo/redo buttons in your html as follows:

     

    And here's a Plunkr showing it in action. To undo all changes you'll need to loop call undoMgr.undoCommand.execute in javascript until all the changes are undone.

提交回复
热议问题