简体   繁体   English

JsViews似乎不适用于数字属性

[英]JsViews doesn't seem to work with numeric properties

When I try to pass JavaScript object with numeric properties 当我尝试传递具有数字属性的JavaScript对象时

{ 1: "One", 2: "Two", 3: "Three" }

Data-binding doesn't render property values, only numbers like in example 数据绑定不呈现属性值,仅呈现示例中的数字

 $.templates("template", "#template"); $.link.template("#row", { 1: "One", 2: "Two", 3: "Three" }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jsviews/0.9.90/jsviews.min.js"></script> <script id="template" type="text/x-jsrender"> <td>{{:1}}</td> <td>{{:2}}</td> <td>{{:3}}</td> </script> <table> <tr id="row"> </tr> </table> 

But if I change property names of object to something beginning with letter it works OK 但是,如果我将对象的属性名称更改为以字母开头的名称,则可以正常工作

 $.templates("template", "#template"); $.link.template("#row", { n1: "One", n2: "Two", n3: "Three" }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jsviews/0.9.90/jsviews.min.js"></script> <script id="template" type="text/x-jsrender"> <td>{{:n1}}</td> <td>{{:n2}}</td> <td>{{:n3}}</td> </script> <table> <tr id="row"> </tr> </table> 

Is it bug or feature? 它是错误还是功能? How to make jsViews work with numeric properties without converting passed object? 如何使jsViews使用数字属性而不转换传递的对象?

If you write {{:4}} for some integer, then JsRender treats that as an expression, and evaluates it. 如果您为某个整数写{{:4}} ,则JsRender会将其视为一个表达式,然后对其求值。 (For example {{:4*12+2}} will render 50 ). (例如{{:4*12+2}}将呈现50 )。

In JavaScript if an object property name (key) is not a valid identifier name you have to use the square bracket accessor syntax. 在JavaScript中,如果对象属性名称(键)不是有效的标识符名称,则必须使用方括号访问器语法。

In JsRender/JsViews templates, the same is true. 在JsRender / JsViews模板中,也是如此。 (See www.jsviews.com/#paths ). (请参阅www.jsviews.com/#paths )。

Here are multiple examples: 这是多个示例:

 $.templates("template", "#template"); $.link.template("#row", { 1: "One", "2": "Two", 3: "Three", other: { 50: "fifty" }, 4: { 5: "five"}, "ab": "AB" }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jsviews/0.9.90/jsviews.min.js"></script> <script id="template" type="text/x-jsrender"> <td>{{:#data[1]}}</td> <td>{{:#data[1+1]}}</td> <td>{{:#data["3"]}}</td> <td>{{:other[50]}}</td> <td>{{:~root[1]}}</td> <td>{{:#data[4]["5"]}}</td> <td>{{:#data["ab"]}}</td> </script> <table> <tr id="row"> </tr> </table> 

Please check JSRender/JSView 请检查JSRender / JSView
Here if you see last section Sample: {{:#index ...}} , it uses index of json object and same is happening when you use numbers as key in json object and try to map it in template, hence template it treating it as index and not key. 在这里,如果您看到最后一节Sample:{{:#index ...}} ,它使用json对象的索引,当您将数字用作json对象中的键并尝试将其映射到模板中时,会发生相同的情况,因此,将其作为模板处理它作为索引而不是键。 Better user some static letter with number like key1, key2.. etc. 更好地为用户提供一些带有数字的静态字母,例如key1,key2等。

 $.templates("template", "#template"); $.link.template("#row", {key1: "One", key2: "Two", key3: "Three" }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jsviews/0.9.90/jsviews.min.js"></script> <script id="template" type="text/x-jsrender"> <td>{{:key1}}</td> <td>{{:key2}}</td> <td>{{:key3}}</td> </script> <table> <tr id="row"> </tr> </table> 

我认为您应该像下面那样使用字符串。

{ "1": "One", "2": "Two", "3": "Three" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM