External observable in viewmodel binding issue

4 posts, 0 answers
  1. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2013

    Posted 06 Oct Link to this post

    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

  2. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 10 Oct Link to this post

    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.
     
  3. Kendo UI is VS 2017 Ready
  4. Michael
    Michael avatar
    8 posts
    Member since:
    Feb 2013

    Posted 10 Oct in reply to Alexander Popov Link to this post

    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

  5. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 12 Oct Link to this post

    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.
     
Back to Top
Kendo UI is VS 2017 Ready