Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Динамическая замена шаблонов Knockout (https://javascript.ru/forum/misc/61295-dinamicheskaya-zamena-shablonov-knockout.html)

CWD 12.02.2016 00:28

Динамическая замена шаблонов Knockout
 
Добрый день.
<script id="template1" type="text/html">
    <h3>Template 1</h3>
    <button id="templButton" data-bind="click: swap">Go to template 2</button>
</script>

<script id="template2" type="text/html">
    <h3>Template 2</h3>
    <button id="templButton" data-bind="click: swap">Go to template 2</button>
</script>

<div data-bind="template: theTemplate"></div>


<script>
    ko.applyBindings({
        theTemplate: ko.observable("template1"),
        swap: function () {
            this.theTemplate("template2");
        }
    });
</script>


Как переделать функцию что бы переключение было взаимное с первого на второй и обратно?

Насколько я понимаю
{
        theTemplate: ko.observable("template1"),
        swap: function () {
            this.theTemplate("template2");
        }
это список обьектов в который необходимо добавить изменения? Должно выглядеть как-то так
ko.applyBindings({
        theTemplate: ko.observable("template1"),
        swap: function () {
            if (this.theTemplate==template1)
            {
                this.theTemplate("template2");
            }
            else
            {
                this.theTemplate("template1");
            }
        }
    });


но что за конструкция
theTemplate("template2");
что делают круглые скобки? Как узнать состояние theTemplate?
Спасибо.


Часовой пояс GMT +3, время: 01:02.