简体   繁体   English

在车把模板中发布渲染收集数据

[英]Issue rendering collection data in handlebars template

I want to render collection in handlebars precompiled templates, this.courses.fetch() is working handlebars view is rendering correctly...(only each loop not rendering collection data) 我想在车把的预编译模板中渲染集合, this.courses.fetch()正在工作,车把视图正在正确渲染...(仅每个循环不渲染集合数据)

here is my code... 这是我的代码...

router.js router.js

App.Router = Backbone.Router.extend({
 routes: {
     'master/courses' : 'courses'
 },
initialize: function(){
    this.courses = new App.Collections.Courses();
     this.list = new App.Views.List( {collection: this.courses} );
},
courses: function() {
    $('#page').html(this.list.render().el);
}
 });

var app = new App.Router();

Backbone.history.start();

courses.js courses.js

App.Collections.Courses = Backbone.Collection.extend({
model: App.Models.Course,
url: '/api/courses'
});

list.js list.js

p.Views.List = Backbone.View.extend({
initialize: function() {
    this.listenTo(this.collection, 'reset', this.render);
},
render:function(){
    this.$el.html( Handlebars.templates.list(this.collection) );
    return this;
}
 });

list.handlebars list.handlebars

<h1>Courses</h1>

<table class="table">
<thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
    </tr>
</thead>
<tbody>
    <tr>
        {{#each models}}
        <td>{{attributes.id}}</td>
        <td>{{attributes.name}}</td>
        {{/each}}
    </tr>
</tbody>
 </table>

this is my first attempt on backbone project kindly suggest good practice also 这是我对骨干项目的第一次尝试,也建议良好实践

I don't see here the point where collection fetching is fired. 我在这里看不到触发集合获取的地步。 The first line in your courses probably should be this.collection.fetch({ reset: true }); courses的第一行可能应该是this.collection.fetch({ reset: true });

As I see in provided HTML structure, each should be before tr tag. 正如我在提供的HTML结构中看到的那样, each应该在tr标记之前。 Like this: 像这样:

<h1>Courses</h1>
<table class="table">
  <thead>
  <tr>
     <th>ID</th>
     <th>Name</th>
  </tr>
  </thead>
  <tbody>
    {{#each models}}
    <tr>
      <td>{{attributes.id}}</td>
      <td>{{attributes.name}}</td>
    </tr>
    {{/each}}
  </tbody>
</table>

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

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