简体   繁体   English

BackboneJS-使用Handlebars.js调用多个集合

[英]BackboneJS - Calling multiple collections with Handlebars.js

How do I fetch and display several backbone collections using Handlebars.js? 如何使用Handlebars.js来获取和显示几个骨干集合? I created 4 json files, each of them are assigned to their own collection and model. 我创建了4个json文件,每个文件都分配了自己的集合和模型。

In my router I have in the initialize: 在我的路由器中,我具有初始化:

initialize: function () {
   this.allcategoryMenuCollection = new AllCategoryMenuCollection();
   this.natcategoryMenuCollection = new NatCategoryMenuCollection();
   this.intcategoryMenuCollection = new IntCategoryMenuCollection();
   this.topcategoryMenuCollection = new TopCategoryMenuCollection();
}

then i defined in the routes, when you click a specific link you will be redirected to a certain page: 然后我在路线中定义,当您单击特定链接时,您将被重定向到某个页面:

newpage: function () {

this.artistpageLeftMenuView = new ArtistpageLeftMenuView({el:'#leftMenu'});
this.artistpageLeftMenuView.collections = {
    allcategoryMenuCollection: this.allcategoryMenuCollection,
    natcategoryMenuCollection: this.natcategoryMenuCollection,
    intcategoryMenuCollection: this.intcategoryMenuCollection,
    topcategoryMenuCollection: this.topcategoryMenuCollection
};
this.artistpageLeftMenuView.render();
}

and my HTML file is: 我的HTML文件是:

<ul class="sbm2">
    {{categoryAll}}
    {{#each .}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

<ul class="sbm2">
    {{categoryNat}}
    {{#each .}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

<ul class="sbm2">
    {{categoryAll}}
    {{#each .}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

<ul class="sbm2">
    {{top100}}
    {{#each .}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

and in my Backbone View i have: 在我的骨干网视图中,我有:

render: function() {
  $(this.el).html(this.template({categoryAll:this.collections.allcategoryMenuCollection.toJSON()}));
  $(this.el).append(this.template({top100:this.collections.topcategoryMenuCollection.toJSON()}));
  $(this.el).append(this.template({categoryNat:this.collections.natcategoryMenuCollection.toJSON()}));
  $(this.el).append(this.template({categoryInt:this.collections.intcategoryMenuCollection.toJSON()}));
  return this;
}

I dont know what it is, but it shows me the html template 4 times and without any content?!?! 我不知道它是什么,但是它向我显示了html模板4次,并且没有任何内容?!?!

I'm not really sure I understood what you're trying to do. 我不太确定我了解您要做什么。 Since my solution would be trivial. 因为我的解决方案将是微不足道的。 I would make the template like this: 我将这样制作模板:

<ul class="sbm2">
  {{#each allcategoryMenuCollection}}
    <li> 
      <a href="{{this.url}}">{{this.name}}</a>
    </li>
  {{/each}}
</ul>
    <ul class="sbm2">
    {{#each natcategoryMenuCollection}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

<ul class="sbm2">
    {{#each intcategoryMenuCollection}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

<ul class="sbm2">
    {{#each topcategoryMenuCollection}}
    <li> 
        <a href="{{this.url}}">{{this.name}}</a>
    </li>
    {{/each}}
</ul>

And render it with: 并使用以下代码渲染:

render: function() {
  $(this.el).html(this.template(this.collections.toJSON()));
  return this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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