knockout-undomanager icon indicating copy to clipboard operation
knockout-undomanager copied to clipboard

Feature Request: Watch Multiple Model/Components

Open GAAOPS opened this issue 9 years ago • 3 comments

Hello, I want to know is there any way to watch multiple view model trough one Undomanager instance?

I have already tried this: vm1={....}, vm2={....} undoVM={v1:vm1,v2:vm2}

is there any other way?

is there anyway to change watched model after UndoManager instantiated? I mean you have:

var undoRedoStack = ko.undoManager(viewModel, { ....}); viewModel.undo = undoRedoStack.undoCommand; viewModel.redo = undoRedoStack.redoCommand;

now i want to change/swap out viewmodel or even watch multiple viewmodel. Is there anyway to do that?

Some more question: Is there any issues with memory leak and performance. For example if my viewmodel has an array of Objects containing more than 2000 instances?

Thanks a lot

GAAOPS avatar Feb 17 '16 20:02 GAAOPS

Well, I guess the answer to all of your questions is to use a single viewmodel and use observables also for other viewmodels...

undoVM = { v1: ko.observable(vm1), v2: ko.observable(vm2) }

This way you can replace a viewmodel with another and it will be "followed".

Supporting multiple viewModels would be much more complicate than simply let people create a composed viewModel, don't you think?

About the 2000 instances, I have to say I don't know: we use knockout-undomanager in voidlabs/mosaico and it works fine and we never identified memory leak. In mosaico we also extend undomanager so to serialize anything, you can look at https://github.com/voidlabs/mosaico/tree/master/src/js/undomanager if you want to see how we do that.

Just keep in mind that undomanager, by default, keeps a reference to your previous objects and their previous values, so whatever object they keep "live" it will stay "live". You can implement your own serializer/deserializer (like the one above) if you want to avoid wasting memory.

bago avatar Feb 17 '16 21:02 bago

Thanks a lot for quick answer. In my case i have dozen components on the page and user can swap them out or bring them in again. Naturally every component has its own viewmodel. In this scenario it would be perfect to be able to register/unregister to Undomanger as needed.(like after component viewmodel is created). Composite viewmodel will work partially. Problem is it wont work if you swap out the observable after applyBindings. Here is a copy of a plunk with compositeVM:

http://plnkr.co/edit/mJxwcjTibSepFt57dDEm?p=preview

if you call "compositeVM.v2(vm3);" before applyBindings it works but after applyBindings it just wont track it. That means the only solution is to dispose the undomanager any recreate it every time some new viewmodel needs to be tracked.

For a last question you said

Just keep in mind that undomanager, by default, keeps a reference to your previous objects and their previous values, so whatever object they keep "live" it will stay "live".

I want to know how i can dispose it properly? In my case each component has a dispose method and knockout by default will call them as needed.

Thanks a lot.

GAAOPS avatar Feb 18 '16 07:02 GAAOPS

I don't think the issue is the composition.

It seems the issue is when you use multiple applybindings.

If I remove the second apply bindings it works fine.

Unfortunately I don't have time to investigate this, but... why don't you simply use only one apply binding and then reference the right viewmodel from the main viewmodel tree in your content? This is what I do in mosaico, I have a big viewmodel that compose the whole layout, and this layout changes a lot during the use of the page (most of it is replaced) and the undo works fine.

Also, I see you use "ko.watch": consider undomanager uses ko.watch and sometimes using multiple ko.watch on the same model breaks things.

About disposal: If you dispose them then you won't be able to undo them. Look at the link I linked above if you want to try to "serialize" your actions. But don't try to fix an issue before you know you have it (did you measured a leak??)

bago avatar Feb 19 '16 10:02 bago