admin管理员组文章数量:1292344
/
This fiddle illustrates my problem.
Say I am binding a JSON onto view with the help of jsrender templates.
var vm = {
foo: {color: "red",otherObjectToMatch:"object"},
testData: [{color: "red"}, {color: "yellow"}, {color: "blue"}]
};
Object vm
has 2 properties-
1) a plain object
2) array of objects.
Template-
<script id="template" type="text/x-jsrender">
<p>
{{:foo.color}}
</p>
<ul>
{{for testData}}
<li>index: {{>color}}</li>
{{/for}}
</ul>
</script>
I want to match from plain object #1
by its property where if its property color matches with the property in loop then will apply some class.
I tried-
<p>
{{:foo.color}}
</p>
<ul>
{{for testData}}
{{if foo.color=={{>color}} }}
<li class='match'>index: {{>color}}</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
</ul>
This is a failed try. I can't find how to match with other objects in jsrender.
http://jsfiddle/tQnVt/621/
This fiddle illustrates my problem.
Say I am binding a JSON onto view with the help of jsrender templates.
var vm = {
foo: {color: "red",otherObjectToMatch:"object"},
testData: [{color: "red"}, {color: "yellow"}, {color: "blue"}]
};
Object vm
has 2 properties-
1) a plain object
2) array of objects.
Template-
<script id="template" type="text/x-jsrender">
<p>
{{:foo.color}}
</p>
<ul>
{{for testData}}
<li>index: {{>color}}</li>
{{/for}}
</ul>
</script>
I want to match from plain object #1
by its property where if its property color matches with the property in loop then will apply some class.
I tried-
<p>
{{:foo.color}}
</p>
<ul>
{{for testData}}
{{if foo.color=={{>color}} }}
<li class='match'>index: {{>color}}</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
</ul>
This is a failed try. I can't find how to match with other objects in jsrender.
Share Improve this question edited Jan 22, 2016 at 18:44 BorisMoore 8,5241 gold badge18 silver badges27 bronze badges asked Jan 22, 2016 at 8:59 ManozManoz 6,61713 gold badges71 silver badges116 bronze badges2 Answers
Reset to default 10You need to write
{{if xxx.foo.color===color}}
where xxx
is the parent data - in your case the vm
you passed in as root data.
(It's all about the 'view hierarchy' - see: http://www.jsviews./#views. See also the specific topic Accessing parent data)
Here are several different ways to get to the data on the parent view:
Access ~root
which is a shortcut helper for the root data:
{{for testData}}
{{if ~root.foo.color===color}}
<li class='match'>index: {{>color}} (match)</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
Create a contextual template parameter ~foo
to pass the foo
property from parent data through to the nested template contexts:
{{for testData ~foo=foo}}
{{if ~foo.color===color}}
<li class='match'>index: {{>color}} (match)</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
Step up through the parent view
objects, and get the data view:
{{for testData}}
{{if #parent.parent.data.foo.color===color}}
<li class='match'>index: {{>color}} (match)</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
Use the view.get()
helper to find the parent of view of type "data"
{{for testData}}
{{if #get("data").data.foo.color===color}}
<li class='match'>index: {{>color}} (match)</li>
{{else}}
<li>index: {{>color}}</li>
{{/if}}
{{/for}}
I added all of them to your jsfiddle: http://jsfiddle/tQnVt/625/
See also this related reply: https://stackoverflow./a/34441881/1054484
Also, you can to use #parent
.
Please see link to the docs.
本文标签: javascriptHow to access parent data properties in JsRender nested templatesStack Overflow
版权声明:本文标题:javascript - How to access parent data properties in JsRender nested templates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741554522a2385072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论