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

Event listeners on child template stops working after update

2 Answers 118 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Denny
Top achievements
Rank 1
Denny asked on 15 Apr 2020, 01:22 AM

I am using MVVM to render a master template and within that I am rendering a child template using the data-template attribute. The child template contains some html that I attach an event to via jQuery.

I'm running into an issue where the event no longer fires in the child template after an update to the model.

My real-world implementation is more complex but I have thrown together a simple example of the issue here:

https://dojo.telerik.com/EbuDubel/2

To reproduce the problem:

1. Click "I always work" notice the alert appears
2. Click "I stop working after updating input" notice the alert appears
3. Change the value of the input text
4. Click "I always work" and notice the alert is still working
5. Click "I stop working after updating input" and notice the alert no longer appears

Is this behavior expected?

 

 

 

2 Answers, 1 is accepted

Sort by
0
Denny
Top achievements
Rank 1
answered on 15 Apr 2020, 03:42 PM

Looks like I found my answer based on this post: https://www.telerik.com/forums/using-this-in-source-binding-recreates-dom-after-changing-any-observable-field#oFz3sf9p2keJUNXb8sAR3w

So using data-bind="source: this" wont work. Instead of binding to "this" I'm just going to add a property to my view model and bind that to the child template. Additionally I found if I need access to the view model data from the child I can use data-bind="value:parent().id"

0
Martin
Telerik team
answered on 16 Apr 2020, 01:45 PM

Hello Denny,

I am glad that you were able to find an appropriate solution. Thank you for sharing it so that other members may benefit from it as well.

Feel free to contact us whenever you have further questions.

Regards,
Martin
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
MVVM
Asked by
Denny
Top achievements
Rank 1
Answers by
Denny
Top achievements
Rank 1
Martin
Telerik team
Share this question
or