如果周围和周围的knockoutjs容器

时间:2012-04-18 15:52:43

标签: knockout.js

我正在尝试按如下方式构建一个表,但是我得到“错误:找不到匹配的结束注释标记:ko if:cellIsStartOfRow”。

我做错了吗?

<table>
    <tbody data-bind="foreach: MyDocs">
        <!-- ko if: cellIsStartOfRow --> 
            <tr class="docsRow">
        <!-- /ko -->  
                <td>

                      <!-- There is more databinding in here - a div containing a textarea and also containing a hyperlink surrounding an image.  I think the contents are irrelevant to my question, but I can post if someone disagrees.-->  

                </td> 

        <!-- ko if: cellIsEndOfRow --> 
            </ tr>
        <!-- /ko --> 

   </tbody>
</table>

这是viewmodel的JS。上面的td的内容有些简化,因为我认为那里的内容相当无关紧要。我在我的页面上调用其他js的函数。 viewmodel本身被分配给在页面上声明的变量。

Type.registerNamespace("HpDocs");


HpDocs.DocsVM = function (data) {
    ko.mapping.fromJS(data, {}, this);


    // add additional properties to each document for presentation
    // purposes
    for (i = 0; i < this.MyDocs().length; i++) {
        var myDoc = this.MyDocs()[i];
        myDoc.docObjectId = "docObject" + myDoc.Id();
        myDoc.textareaId = "ucHpDocs" + "_txta";

        if (i % 5 == 0) {
            myDoc.cellIsStartOfRow = true;
            myDoc.cellIsEndOfRow = false;
        } else if (i % 5 == 5) {
            myDoc.cellIsStartOfRow = false;
            myDoc.cellIsEndOfRow = true;
        } else {
            myDoc.cellIsStartOfRow = false;
            myDoc.cellIsEndOfRow = false;
        }
    }
};

HpDocs.DocsVM.prototype = {
//    cellIsStartOfRow: function(){
//        return true;
//    },
    getDocs: function (filter) {
        var self = this;
        $.ajax({
            url: getMethodUrl("GetDocs"),
            data: "{'filter': " + filter + "}",
            success: function (response) {
                ko.mapping.fromJS(response.d, {}, self.MyDocs);
            }
        })
    }
};

HpDocs.dbGetDocs = function (filter) {
    $.ajax({
        url: getMethodUrl("DbGetDocs"),
        data: "{'filter': " + filter + "}",
        success: function (response) {

            myDocsViewModel = new HpDocs.DocsVM({
                MyDocs: ko.mapping.fromJS(response.d)
            });

            var bindingScope = $("#divMyDocs")[0];
            ko.applyBindings(myDocsViewModel, bindingScope);

            $(".DocsUpdateProgress").addClass("invisible");
        }
    })
};


HpDocs.getPreferredTab = function () {
    var tabPref = $("[id$='hidDocTabPreference']").html();
    return tabPref;
};

HpDocs.showProgress = function () {
    $(".DocsUpdateProgress").removeClass("invisible");
};
HpDocs.hideProgress = function () {
    $(".DocsUpdateProgress").addClass("invisible");
};



//register the class
HpDocs.DocsVM.registerClass('HpDocs.DocsVM', null, Sys.IDisposable);


// notify ajax that the script is now loaded.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

ANSWER

我重构了我的模型:我现在让它包含一个名为Rows的属性,而不是让MyDocs包含一个对象列表,而Rows又包含一个名为Documents的属性。然后,我可以做以下事情:

<table id="tblMyDocs">
                <tbody data-bind="foreach:  MyDocs.Rows">

                        <tr data-bind="foreach: Documents">
                                  <td>
                                          <!-- in here i present each document by databinding to the Model's properties -->
                                  <td>
                        </tr>
                 </tbody>
</table>

当然,视图模型更容易,因为模型现在按行组织:

HpDocs.DocsVM = function (data) {
    ko.mapping.fromJS(data, {}, this);
};

HpDocs.DocsVM.prototype = {
    getDocs: function (filter) {
        var self = this;
        $.ajax({
            url: getMethodUrl("GetDocs"),
            data: "{'filter': " + filter + "}",
            success: function (response) {
                ko.mapping.fromJS(response.d, {}, self.MyDocs);
            }
        })
    }
};

HpDocs.dbGetDocs = function (filter) {
    $.ajax({
        url: getMethodUrl("DbGetDocs"),
        data: "{'filter': " + filter + "}",
        success: function (response) {

            myDocsViewModel = new HpDocs.DocsVM({
                MyDocs: ko.mapping.fromJS(response.d)
            });

            var bindingScope = $("#divMyDocs")[0];
            ko.applyBindings(myDocsViewModel, bindingScope);

            HpDocs.hideProgress();
        }
    })
};

2 个答案:

答案 0 :(得分:2)

问题在于:

<!-- ko if: cellIsStartOfRow --> 
   <tr class="docsRow">
<!-- /ko -->  

你必须在if中包含open和close标签,所以..

<!-- ko if: cellIsStartOfRow --> 
   <tr class="docsRow">
   ... 
   </tr>
<!-- /ko -->  

答案 1 :(得分:1)

而不是像下面那样为你的js添加格式化逻辑:

// add additional properties to each document for presentation
    // purposes
    for (i = 0; i < this.MyDocs().length; i++) {
        var myDoc = this.MyDocs()[i];
        myDoc.docObjectId = "docObject" + myDoc.Id();
        myDoc.textareaId = "ucHpDocs" + "_txta";

        if (i % 5 == 0) {
            myDoc.cellIsStartOfRow = true;
            myDoc.cellIsEndOfRow = false;
        } else if (i % 5 == 5) {
            myDoc.cellIsStartOfRow = false;
            myDoc.cellIsEndOfRow = true;
        } else {
            myDoc.cellIsStartOfRow = false;
            myDoc.cellIsEndOfRow = false;
        }

我建议为数据行创建一个单独的viewmodel。由于你没有提供任何json数据,我无法100%解决它,但希望这能让你朝着正确的方向前进。这是我正在处理的jsfiddle:http://jsfiddle.net/JasonMore/GcSAn/2/

查看

<table>
    <tbody data-bind="foreach: someArray">
            <tr class="docsRow" data-bind="foreach:docRows">
                <td>
                    <div data-bind="attr:  {id: objectId}">
                       <a data-bind="attr: {href: someUrl}">
                          <img data-bind="attr: {src: IconPath, alt: Tooltip}"/>
                       </a> 
                       <br/>
                       <textarea runat="server" readonly="readonly" data-bind="html: DisplayName"></textarea>

                    </div>
                </td> 
            </ tr>

   </tbody>
</table>​​​​​

<强>的Javascript

HpDocs.RowVM = function(data) {
    ko.mapping.fromJS(data, {}, this);

}

HpDocs.DocsVM = function(data) {
    ko.mapping.fromJS(data, {}, this);

    this.docRows = ko.observableArray();

    // add additional properties to each document for presentation
    // purposes
    for (i = 0; i < this.MyDocs().length; i++) {
        var myDoc = this.MyDocs()[i];
        myDoc.docObjectId = "docObject" + myDoc.Id();
        myDoc.textareaId = "ucHpDocs" + "_txta";

        if (i % 5 == 0) {
            // create new RowVM and start adding DocsVM to it
        } else if (i % 5 == 5) {
            // push the RowVM to this.docRows populated with cells
        } else {
            // add the myDoc to the current RowVM you are working with
        }
    }
};

更新 - 关于如何创建合适的mvvm的淘汰赛的链接

幻灯片和示例:http://bit.ly/FamilyFeudSlides

代码:https://github.com/jasonmore/familyfeud