淘汰外部模板值重置

时间:2015-06-12 10:05:26

标签: knockout.js knockout-templating

使用Knockout外部模板 https://github.com/ifandelse/Knockout.js-External-Template-Engine

但是如何从外部模板中清除新绑定的值和文本。 我的HTML代码是这样的

HTML

<ul data-bind="template: { name: 'RegistrationForm.html' }"></ul>
 <div data-bind="click:Add"></div>    

脚本 当我点击添加,来自&#39; RegistrationForm.html&#39;将绑定到网格,但同时我需要清除来自&#39; RegistrationForm.html&#39;的所有绑定值。 假设我在所选模板上有许多字段和一些文本绑定。 所以我的问题是如何在点击按钮时清除模板中的绑定文本和表单值。

实际情景:

  

打开用户模板时,然后输入值和文本   与相应用户相关的将绑定到该模板。但当   我点击下一个用户,没有任何值,第一个用户值仍然存在   那里。我为所有用户使用一个模板)

谢谢

修改 我的代码就像这个结构

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Templates</title>
        <style>
            .clear {
                clear: both;
            }
        </style>
        <script type="text/javascript" src="lib/jquery-1.11.2.min.js"></script>
        <script type="text/javascript" src="lib/knockout-3.3.0.js"></script>
        <script type="text/javascript" src="lib/koExternalTemplateEngine_all.js"></script>
        <script>
                    $(function(){
                    function pop1(data)
                    {
                    this.a = ko.obeservable(data.a || '');
                            this.b = ko.obeservable(data.b || '');
                    }
                    function pop2(data)
                    {
                    this.c = ko.obeservable(data.c || '');
                            this.d = ko.obeservable(data.d || '');
                    }
                    Viewmodel = function(){
                    self = this;
                            self.obj = new Object();
                            self.name = {
                            id:1
                                    name:'test'
                            };
                            self.types = [
                            {
                            id:1
                                    name:'test'
                            },
                            {
                            id:2
                                    name:'test2'
                            }

                            ];
                            self.whichPopup = fucntion(){
                    switch (name()){
                    case '1':
                            //Load Template for this option
                            //This wil b a user form with some value will bind when on laod
                            var data {}; // data with a and b
                            self.obj = new pop1(data);
                            break;
                            case '2':
                            //Load Template for this option
                            var data {}; // data with c and d
                            self.obj = new pop1(data);
                            break;
                    }
                    };
                    };
                    }); </script>
    </head>
    <body>
        <div class="Temp">
            <div class="temp1">  
                <div data-bind="template:{name : 'Template1.html'}"></div>  
            </div>
            <div class="clear"></div>
            <div class="temp2">                 
                <div data-bind="template:{name : 'Template2.html'}"></div>              
            </div>  
            <div class="clear"></div>
            <select class="submission_type"
                    data-bind="options: types,
                                   optionsText : 'name',                               
                                   value: name,
                                   optionsCaption:'---Select---'
                    ">
            </select>
            <div data-bind="template:{name : whichPopup}"></div>
            <!--  This part working -->
            <!--
             Table here
             i need to add multiple entried here,
            so will add users here

             1- Select type
             2- Tempalte will opened in a popup
             3- Save to,then dispaly to the grid
            -->


            <!--
              1- Selcted option type that i hav already added before for to add same type but diffrent user
              2- popup1 will opened
              3- but it have the values that i have first time added
              4- if it was text box only then i can clear but it have some text binding to a div too
            -->
        </div>  

    </body>
</html>

更新 我已经更改了这个概念,让我知道如何在knockout外部模板中禁用缓存模板。 到目前为止,我已经尝试过:

infuser.defaults.ajax.cache = false

但不工作,我只需要刷新模板。 谢谢

0 个答案:

没有答案