Knockout component nested template writing notes, knockout nesting

Source: Internet
Author: User

Knockout component nested template writing notes, knockout nesting

When writing a Knockout component nested template, multiple quotation marks are used, especially when single quotation marks and double quotation marks are used simultaneously. Here is an example [1 ]:

    templateEngine.addTemplate("ko_simpleGrid_grid", "\            <table class=\"ko-grid\" cellspacing=\"0\" data-bind=\"\">\                <thead>\                    <tr data-bind=\"foreach: columns\">\                       <th><div><span class=\"adjustCol_left\"> </span><span data-bind=\"text: headerText\"></span> \                       <span class=\"adjustCol_right\" data-bind=\"event:{mousedown:$parent.headTdMousedown}\"> </span></div></th>\                    </tr>\                </thead>\                <tbody data-bind=\"foreach: itemsOnCurrentPage\">\                   <tr data-bind=\"foreach: $parent.columns\">\                       <td data-bind=\"attr:{ prop1:typeof rowText == 'function' ? rowText($parent) : $parent[rowText] }, click:$root.cellEdit \"> \                       <div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == \"function\" ? rowText($parent) : $parent[rowText], editing:false}}'></div>\                       </td>\                    </tr>\                </tbody>\            </table>");

Here, pay attention to this line:

(1) correct writing

<div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == \"function\" ? rowText($parent) : $parent[rowText], editing:false}}'></div>\
The value assignment logic is: to determine whether the rowText Data Type of the table model's parent model is a function. If it is a function, execute this function to obtain the value; otherwise, use the value in the parent model. Here, the entire line of string is contained in the large template string. data-bind uses single quotes to write the bound content. In this case, if single quotes are used in the binding, escape the string, for example, upstream (1 ).

If it is written like this, it is wrong:

(2) incorrect syntax

<div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == 'function' ? rowText($parent) : $parent[rowText], editing:false}}'></div>\
Although this is a problem that should be paid attention to, it is easy to forget to escape in complicated scenarios, while incorrect writing leads to very difficult to query errors.

Record your experience as a reference when you encounter similar problems.


Refer:

Page Grid: http://knockoutjs.com/examples/grid.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.