重复使用手柄查找 [英] Using Handlebars lookup with repetition

查看:111
本文介绍了重复使用手柄查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个对象数组,我想使用一个嵌套对象的一个​​属性来查找Handlebars中关联对象中的各种属性。



在这个例子中,我想列出每个大学的学生名单以及每个学生所属部门的信息。

我的代码可以工作,但是嵌套的查询非常有用重复:

  {{lookup(lookup ../majors major)'dean'}} $ b $ {{lookup( lookup ../majors major)'location'}} 

我能做些什么吗?我想访问查找的上下文,如下所示:

  {{#lookup ../majors major }} 
{{dean}}
{{location}}
{{/ lookup}}

var source = $(#hb-template)。html(); var template = Handlebars (html);

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js> ;< / script>< script src =https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js>< / script><脚本id =hb-templatetype =text / x-handlebars-template> {{#universities}}< h1> {{name}}< / h1> {{#students}}< h2> {{name}}< / h2> < DL> <峰; dt>主要< / DT> <峰; dd> {{主要}}< / DD> < dt> Department dean< / dt> < dd> {{lookup(lookup ../majors major)'dean'}}< / dd> < dt>部门位置< / dt> < dd> {{lookup(lookup ../majors major)'location'}}< / dd> < / dl的> {/ students}} {{/ universities}}< / script>< div id =hb-html>< / div>< script> var context = {schools:[{name:Example University,students:[{name:Alice,major:Business},{name ,major:English}],majors:{English:{dean:Dr. Smith,location:101室}, Dr. Jones,location:房间999}}},{name:另一所大学,学生:[{名称:鲍勃,主要:商业}} ],majors:{Business:{dean:Dr. Zimmerman,location:South Campus}}}]};< / script>  

解决方案

以下是我的解决方案:使用 {{#with}} helper 并将查找子表达式的结果(一个对象)传递给它。我不确定这是否是最好的方法,或者真的如此,为什么会喜欢评论。

(lookup ../majors major)}}
< dt>院系院长< / dt>
< dd> {{dean}}< / dd>
< dt>部门地点< / dt>
< dd> {{location}}< / dd>
{{/ with}}

完整示例:

var source = $(#hb-template)。html(); var template = Handlebars.compile (source); var html = template(context); $(#hb-html)。html(html);

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js><<< ; / script>< script src =https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js>< / script>< script id = hb-templatetype =text / x-handlebars-template> {{#universities}}< h1> {{name}}< / h1> {{#students}}< h2> {{name}}< / h2> < DL> <峰; dt>主要< / DT> <峰; dd> {{主要}}< / DD> {{#with(lookup ../majors major)}}< dt> Department dean< / dt> <峰; dd> {{院长}}< / DD> < dt>部门位置< / dt> <峰; dd> {{位置}}< / DD> {{/ with}}< / dl> {/ students}} {{/ universities}}< / script>< div id =hb-html>< / div>< script> var context = {schools:[{name:Example University,students:[{name:Alice,major:Business},{name ,major:English}],majors:{English:{dean:Dr. Smith,location:101室}, Dr. Jones,location:房间999}}},{name:另一所大学,学生:[{名称:鲍勃,主要:商业}} ],majors:{Business:{dean:Dr. Zimmerman,location:South Campus}}}]};< / script>  pre> 


Given an array of objects, I would like to use one property of a nested object to look up various properties in an associated object in Handlebars.

In this example, I would like to show a list of students at each university, and information about the department to which each student belongs.

My code works, but the nested lookups are very repetitive:

{{lookup (lookup ../majors major) 'dean'}}
{{lookup (lookup ../majors major) 'location'}}

Is there anything I can do about this? I'd like to do access the context of the lookup, something like this:

{{#lookup ../majors major}}
    {{dean}}
    {{location}}
{{/lookup}}

var source = $("#hb-template").html();
var template = Handlebars.compile(source);
var html = template(context);
$("#hb-html").html(html);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>

<script id="hb-template" type="text/x-handlebars-template">
  {{#universities}}
  <h1>{{name}}</h1>
  {{#students}}
  <h2>{{name}}</h2>
  <dl>
    <dt>Major</dt>
    <dd>{{major}}</dd>
    <dt>Department dean</dt>
    <dd>{{lookup (lookup ../majors major) 'dean'}}</dd>
    <dt>Department location</dt>
    <dd>{{lookup (lookup ../majors major) 'location'}}</dd>
  </dl>
  {{/students}}
  {{/universities}}
</script>

<div id="hb-html">
</div>

<script>
  var context = {
    "universities": [{
        "name": "Example University",
        "students": [{
            "name": "Alice",
            "major": "Business"
          },
          {
            "name": "John",
            "major": "English"
          }
        ],
        "majors": {
          "English": {
            "dean": "Dr. Smith",
            "location": "Room 101"
          },
          "Business": {
            "dean": "Dr. Jones",
            "location": "Room 999"
          }
        }
      },
      {
        "name": "Another University",
        "students": [{
          "name": "Bob",
          "major": "Business"
        }],
        "majors": {
          "Business": {
            "dean": "Dr. Zimmerman",
            "location": "South Campus"
          }
        }
      }
    ]
  };
</script>

解决方案

Here is my solution: use the {{#with}} helper and pass it the result of the lookup subexpression—an object. I'm not sure if this is the best method, or really why this works, so would appreciate comments.

{{#with (lookup ../majors major)}}
    <dt>Department dean</dt>
    <dd>{{dean}}</dd>
    <dt>Department location</dt>
    <dd>{{location}}</dd>
{{/with}}

Full example:

var source = $("#hb-template").html();
var template = Handlebars.compile(source);
var html = template(context);
$("#hb-html").html(html);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.6/handlebars.min.js"></script>

<script id="hb-template" type="text/x-handlebars-template">
  {{#universities}}
  <h1>{{name}}</h1>
  {{#students}}
  <h2>{{name}}</h2>
  <dl>
    <dt>Major</dt>
    <dd>{{major}}</dd>
    {{#with (lookup ../majors major)}}
    <dt>Department dean</dt>
    <dd>{{dean}}</dd>
    <dt>Department location</dt>
    <dd>{{location}}</dd>
    {{/with}}
  </dl>
  {{/students}}
  {{/universities}}
</script>

<div id="hb-html">
</div>

<script>
  var context = {
    "universities": [{
        "name": "Example University",
        "students": [{
            "name": "Alice",
            "major": "Business"
          },
          {
            "name": "John",
            "major": "English"
          }
        ],
        "majors": {
          "English": {
            "dean": "Dr. Smith",
            "location": "Room 101"
          },
          "Business": {
            "dean": "Dr. Jones",
            "location": "Room 999"
          }
        }
      },
      {
        "name": "Another University",
        "students": [{
          "name": "Bob",
          "major": "Business"
        }],
        "majors": {
          "Business": {
            "dean": "Dr. Zimmerman",
            "location": "South Campus"
          }
        }
      }
    ]
  };
</script>

这篇关于重复使用手柄查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆