在车把模板问题渲染收集数据 [英] Issue rendering collection data in handlebars template

查看:89
本文介绍了在车把模板问题渲染收集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要呈现收集车把precompiled模板,
this.courses.fetch()是工作
车把观点正确渲染...(仅限每个循环不渲染收集数据)

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)

这是我的code ...

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

推荐答案

我不认为在这些地方集合取触发点。在你的课程的第一行大概应该是 this.collection.fetch({重置:真});

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 });

我在提供HTML结构看,每个 TR 标记之前。像这样的:

As I see in provided HTML structure, each should be before tr tag. 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>

这篇关于在车把模板问题渲染收集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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