Is there a way to dispose ViewModels created by BindKeyedList? #57
-
I've got a ViewModel with a BindKeyedList creating ViewModels for the items of a list, so like
When the TopViewModel is disposed, is there a way to have the items in ListItems disposed too? |
Beta Was this translation helpful? Give feedback.
Answered by
JordanMarr
Nov 21, 2024
Replies: 2 comments
-
I think you should be able to add them to the disposables list on your VM: For example: type ActionVM(action: Action) =
inherit ReactiveElmishViewModel()
member this.Timestamp = action.Timestamp
member this.Description = action.Description
interface IDisposable with
member this.Dispose() = printfn "Disposing ActionVM"
type CounterViewModel() as this =
inherit ReactiveElmishViewModel()
let local =
Program.mkAvaloniaSimple init update
|> Program.mkStore
let mkActionVM action =
let vm = new ActionVM(action)
this.AddDisposable(vm)
vm
member this.Count = this.Bind(local, _.Count)
member this.Actions =
this.BindList(
local
, _.Actions
, map = mkActionVM
, sortBy = _.Timestamp
)
member this.Increment() = local.Dispatch Increment
member this.Decrement() = local.Dispatch Decrement
member this.Reset() = local.Dispatch Reset
member this.IsResetEnabled = this.Bind(local, fun m -> m.Actions.Length > 1)
static member DesignVM =
new CounterViewModel() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
marklam
-
Many thanks for that!. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you should be able to add them to the disposables list on your VM:
ReactiveElmish.Avalonia/src/ReactiveElmish/ReactiveElmishViewModel.fs
Line 413 in 4320bc9
For example: