Relying on automatic computed observables for component parameters triggers dispose

39 views
Skip to first unread message

mcl...@newvistas.com

unread,
Jan 13, 2017, 4:51:20 PM1/13/17
to KnockoutJS
I have a `popup` component, various params, used various places. I'm also trying to make sure I dispose appropriately of unused data to prevent memory leaks - still a bit confused about exactly when dispose is called generally, some more documentation about cleaning up might be helpful - though some of my confusion has been caused by it being called on my view models when the HTML element is still on the page, possibly related.

Regardless - I was passing `foo.bar.observable() && foo.bar.observable().observableProperty()` as a param to the `popup` component, which was immediately triggering `dispose`. After extensive `console.log` statements in knockout's internals, I discovered if I instead pass `foo.bar.baz` and set `self.baz = ko.pureComputed(function() { return self.observable() && self.observable().observableProperty() });`, `dispose` is not called immediately.

Is this immediate call to `dispose` the intended behavior? If so, is there some nifty trick this enables that is non-intuitive to me?

Michael Best

unread,
Jan 15, 2017, 1:44:40 AM1/15/17
to KnockoutJS
Can you provide a little more context, perhaps a working example in jsfiddle?

Thanks

mcl...@newvistas.com

unread,
Jan 17, 2017, 5:14:53 PM1/17/17
to KnockoutJS

Michael Best

unread,
Jan 19, 2017, 4:06:55 PM1/19/17
to KnockoutJS
When using the component binding directly, the parameter values are first calculated and then passed to the viewmodel constructor. If any value changes later, a new viewmodel is constructed and the previous one disposed.

If you use the custom element syntax, parameters are each wrapped in a computed (if necessary) and then passed to the viewmodel constructor, so the above type of update doesn't happen. https://jsfiddle.net/629fdrek/2/

Why the difference? See https://github.com/knockout/knockout/pull/1385

This issue is currently being discussed: https://github.com/knockout/knockout/issues/1741
Reply all
Reply to author
Forward
0 new messages