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

Span not updated by MVVM

2 Answers 355 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 24 Feb 2016, 07:26 PM

Hi

 

I've been trying out the MVVM system for an upcoming project, and I seem to have a problem with understanding the basics. All of the examples/samples I've tried from the Kendo webpage have worked as intended, but my own test refuses to function.

Basically I have a span with a text value, which is changed by a button click. Simple. The problem is that, even though the ViewModel is updated (according to console print-outs), the text being displayed on the website does not.

I hope someone could have a look at this and tell me what I'm doing wrong - really is a very simple example.

 

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
 
 
</head>
<body>
 
    <div id="app">
        <div id="replaceMe"></div>
    </div>
    <div>
        <button id="swapButton">Swap Text</button>
    </div>
 
    <script type="text/x-kendo-template" id="template">
        <div>
            <span data-bind="text:displayMe">Not bound</span>
        </div>
    </script>
 
    <script type="text/javascript">
 
        var viewModel = kendo.observable({
            displayMe: "Foo",
            swapText: function () {
                if (this.displayMe == "Foo") {
                    this.displayMe = "Bar";
                } else {
                    this.displayMe = "Foo";
                }
 
            }
        });
 
        $("#swapButton").kendoButton({
            click: function (e) {
                viewModel.swapText();
                console.log("Swapped Text: " + viewModel.displayMe);
            }
        });
 
        var template = kendo.template($("#template").html(), { useWithBlock: false });
        var templateAsHtml = template({});
         
        $("#replaceMe").html(templateAsHtml);
        kendo.bind($("#replaceMe"), viewModel);
 
    </script>
</body>
</html>

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 25 Feb 2016, 09:38 AM
Hi,

you need to use the set method when changing values.

Regards,
Petyo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Andreas
Top achievements
Rank 1
answered on 25 Feb 2016, 09:43 AM

Snap. Knew it was something simple.

Thank you for the help Petyo - works like a charm now.

Tags
MVVM
Asked by
Andreas
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Andreas
Top achievements
Rank 1
Share this question
or