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

Using "this" in source binding recreates DOM after changing any observable field

1 Answer 62 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 31 Aug 2012, 10:45 AM
Hi!
I have following issue: if "this" keyword used in source binding, MVVM recreates the whole DOM starting from this node if any child value of "this" observable model changes. here is a fiddle to reproduce it:
http://jsfiddle.net/uBgnr/21/ 

If instead of "this" model member is used, DOM is not recreated, here is another fiddle:
http://jsfiddle.net/uBgnr/20/

In this case I could avoid this behavior, but I have following scenario and I could not change data structure:
I have an array of elements to be bind in a div. But the problem is: each of element has it's own template, stored in field "template", so I could do following:
<div data-template="element" data-bind="source:elements"></div>
<script id="element" type="text/x-kendo-template">  
    <div data-template="#=template#" data-bind="source:this"></div>
</script>

this will work, but as I said if any child field of  my view model will be changed, MVVM recreates div with "source:this" binding

using "#if" inside element template is not an option, templates are huge, and could be used also not only within "elements" binding, so I need to copy/paste them, its dirty. 

Suggestion: it would be nice something like this:
<div data-template-field="template" data-bind="source:elements">

Please help.

1 Answer, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 05 Sep 2012, 08:44 AM
Hello Alex,

By design if a template is bound to the entire viewModel object (data-bind="source: this") on change of a child value the framework will refresh the whole template DOM. Thus is because when you set the value of a child property the change event of the viewModel object fires.
I am afraid that I cannot suggest another workaround except the one you already found - to bind the template to a model member.

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
MVVM
Asked by
Alex
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Share this question
or