简体   繁体   中英

JSrender: cannot access parent variable inside if condition

as the title says, the problem is clear:

within the construction:

{{if is_completed == 1}}
 <div>
   <p>{{:#parent.parent.data.myproperty}}</p>
 </div>
{{/if}}

the parent properties are not visible!

I solved it by creating construction like:{{for movies ~myproperty=myproperty}} in parent loop, and ~myproperty is visible inside IF conditions, but what if I have several variables, what if I have many-level nesting data structure?

The {{if ...}} block adds another view, so means you need to add a .parent to step up through that view, as in: {{:#parent.parent.parent.data.myproperty}}

You can pass in variables as you said ( ~myproperty=... ) and they will be visible to any depth of nesting.

Your variable can be an object too such as the current data object: ~myObj=#data :

{{sometag a=b ~myObj=#data}}
    ....
    {{:~myObj.myproperty}}
    ...
{{/sometag}}

so you don't need a separate variable for each property.

You can also access the top-level data object and drill down from there:

{{:~root.foo...myproperty}} .

And finally you can use #get("item") to step up through any number of {{if}} blocks and get the nearest "item" view (ie the item view for a repeating {{for ...}} block).

So you would write:

{{:#get("item").data.myproperty}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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