English 中文(简体)
如何在Handlebars / Ember.js 中插入动态的 property?
原标题:How do you interpolate a dynamic {{property}} in Handlebars / Ember.js?

假设我在 JavaScript 中有一个 < code> user 模型, 看起来像这样 :

var User = function(attributes) {
  this.attributes = attributes;
}

User.fields = [
  {name:  firstName },
  {name:  lastName },
  {name:  email }
]

User.prototype.get = function(key) {
  return this.attributes[key];
}

User.all = [new User({firstName:  Foo })];

我想通过一个手动栏模板运行它, 它在 < code> User 类上的每个字段中运行, 为它创建一个页眉, 然后对每个用户设定值 :

<table>
  <thead>
    <tr>
      {{#each User.fields}}
      <th>{{name}}</th>
      {{/each}}
    </tr>
  </thead>
  <tbody>
    {{#each User.all}}
    <tr>
      {{#each User.fields}}
      <td>{{content.get(name)}}</td>
      {{/each}}
    </tr>
    {{/each}}
  </tbody>
</table>

我的问题是,我如何实现这个内部部分:

{{#each User.fields}}
<td>{{content.get(name)}}</td>
{{/each}}

s 基本上是在做 user.get(field.name) 。在手头的字段我不知道,所以我如何才能做到?

谢谢你的帮助

最佳回答
 <body>
   <div id= displayArea ></div>
   <script id="template" type="text/x-handlebars-template">
    <table border="2">
        <thead>
        <tr>
            {{#each Fields}}
             <th>{{name}}</th>
            {{/each}}
        </tr>
        </thead>
        <tbody>
          {{#each users}}
          <tr>
            {{#each ../Fields}}
           <td>{{getName name ../this}}</td>
            {{/each}}
          </tr>
         {{/each}}
        </tbody>
     </table>
 </script>

<script type="text/javascript">
    var User = function(attributes) {
        this.attributes = attributes;
    }

    User.fields = [
        {name:  firstName },
        {name:  lastName },
        {name:  email }
    ]

    User.prototype.get = function(key) {
       return this.attributes[key];
    }

    User.all = [new User({firstName:  Foo ,lastName : ooF ,email :  [email protected] }) , new User({firstName:  Foo2 })];       //array of user

    //handle bar functions to display
    $(function(){
       var template = Handlebars.compile($( #template ).html());

        Handlebars.registerHelper( getName ,function(name,context){
                          return context.get(name);
          });
        $( #displayArea ).html(template({Fields :User.fields,users:User.all}));
    });
   </script>
  </body>  

这将用JS处理器中的帮手解决我们的问题

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签