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