View helpers for循环中的JsViews/JsRender临时/上下文辅助变量
我试图在for循环中存储一个临时/上下文变量,以便以后在另一个for循环中使用。我用它作为参考View helpers for循环中的JsViews/JsRender临时/上下文辅助变量,view-helpers,jsrender,jsviews,View Helpers,Jsrender,Jsviews,我试图在for循环中存储一个临时/上下文变量,以便以后在另一个for循环中使用。我用它作为参考 {^{for instances ~templateId=templateId}} {{:~templateId}} <select data-link="templateId" class="selected-visible" name="select-template"> {^{for ~root.templates}} <
{^{for instances ~templateId=templateId}}
{{:~templateId}}
<select data-link="templateId" class="selected-visible" name="select-template">
{^{for ~root.templates}}
<option data-link="{:name} value{:id} selected{:id == ~templateId}"></option>
{{/for}}
</select>
{{/for}}
现在,当我没有在for循环中设置该值时,它会被打印出来,但是当我在for循环中设置它时,它会再次消失
下一个问题可能是~templateId helper在~root作用域for循环中不可用,因为helper应该只在instances循环的子视图中可用
最终目标是在select中选择正确的值,因此如果有其他解决方案可用,请告诉。您需要从模板中删除
~templateId=templateId
说明:
语法~helper
用于访问帮助程序/上下文参数,这些参数可以是传入/外部注册,如这里所示,或可以在模板中创建/设置,如链接到{for movies~theater=theater}
的示例所示,或者在这个例子中:~frstNm=firstName
:
因此,通常情况下,您要么传入一个助手,要么在模板中创建它—而不是两者都传入
在上面的示例中,您首先将
~templateId
作为0
传递到中,然后使用~templateId=templateId
将其重新定义为上下文参数(实际上是将其值设置为未定义,因为..=templateId
将其设置为当前数据的templateId属性的值-未定义,在您的情况下).谢谢您的回答。也许我的问题还不够清楚,但我已经尝试了作为我的第一种方法,只在模板中设置值。然后它不会使用{:~templateId}打印任何内容……我不知道如何在“viewmodel”中外部设置for循环每次迭代的值。如果templateId是在实例项上定义的,那么您需要在实例循环中设置它-因此数据就是该项。因此您需要{^{for~root.templates~templateId=templateId}}},而不是在{^{for instances…}上进行设置。您只需要将它传递到{^{for~root.templates}块中。。。
$.views.helpers({templateId: 0});