This is a migrated thread and some comments may be shown as answers.

External observable in viewmodel binding issue

3 Answers 146 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 06 Oct 2016, 12:35 PM

Hello!

we've currently searching for a reason why there are getting more and more event handler on an observable object we are using in different view models. 

http://jsbin.com/xujeteqivu/1/edit?html,js,output

In our opinion the change and get events should be unbound from the obs Observable after the unbind of the view model. With more view models which use the obs Observable the amount of events is getting bigger and bigger an the view model never gets collected from the garbage collector.

Thanks in advance!

Michael

3 Answers, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 10 Oct 2016, 12:50 PM
Hello Michael,

Indeed, wrapping an Observable in another Observable has this effect, because the parent must listen to the child's change and get events. This is not related to the binding itself and this is why the unbind and destroy methods are not removing the event handlers. For example: 
var obs = kendo.observable({
  title: ""
});
 
// Console log on change to log count
obs.bind("change", function() {
  console.log("change", this._events);
});
 
// Initial set title - bound change[1]
obs.set("title", "MyTitle1");
 
var viewModel = kendo.observable({
  obs: obs
});
 
console.log("AFTER WRAPPING", obs._events);
// >>>> AFTER WRAPPING Object {change: Array[2], get: Array[1]}

I am afraid removing the nested Observable's event handlers is not supported, as it is a separate instance and we cannot determine when and if this should happen.

Regards,
Alexander Popov
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
0
Michael
Top achievements
Rank 1
answered on 10 Oct 2016, 01:17 PM

Hello Alexander,

i think it should not happen automatically but a method to clean up all handlers like viewModel.destroy() could help. I also think that the use of nested Observables is a common use case and unfortunately many of our implementations base on this feature. 

Regards

Michael

0
Alexander Popov
Telerik team
answered on 12 Oct 2016, 08:26 AM
Hello again Michael,

The use of nested Observables alone is not an issue, however nesting and re-using an instance in another one is causing the behavior you described. What you are after is not laid out in the current design of the Observables, leading to results that you would not expect. I am afraid there is no good workaround other than avoiding this pattern. I would also suggest submitting a feature request on our feedback portal.

Regards,
Alexander Popov
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
MVVM
Asked by
Michael
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or