2012-07-24 75 views
1

关于我的应用程序:
- 我使用Rails 3.2.6与backbone.js(backbone-on-rails gem)和句柄模板引擎。
- 创建了路线和视图,效果很好。我的观点:Rails with Backbone.js and Handlebars

el: $('#lorem'), 
    render: function(){ 
    var js = this.collection.toJSON(); 
    var template = Handlebars.compile($("#lorem2").html()); 
    $(this.el).html(template({articles: js})); 
    console.log(js); 
    return this; 
    } 

-I创建的模板(资产DIR:资产/模板/民族/ index.hbs):

<script id="lorem2" type="text/x-handlebars-template"> 
    {{#each articles}} 
     {{this.name}} 
    {{/each}} 
</script> 

当我刷新页面,我得到这个错误信息:

遗漏的类型错误:无法调用空的“匹配”

我觉得模板文件可能错误:

<script src="/assets/templates/people/index.js?body=1" type="text/javascript"></script> 

这包含:

(function() { 
      this.HandlebarsTemplates || (this.HandlebarsTemplates = {}); 
      this.HandlebarsTemplates["people/index"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) { 
    helpers = helpers || Handlebars.helpers; 
    var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression; 


    buffer += "<div class=\"entry\">\n <h1>"; 
    foundHelper = helpers.title; 
    stack1 = foundHelper || depth0.title; 
    if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); } 
    else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "title", { hash: {} }); } 
    buffer += escapeExpression(stack1) + "</h1>\n <div class=\"body\">\n "; 
    foundHelper = helpers.body; 
    stack1 = foundHelper || depth0.body; 
    if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); } 
    else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "body", { hash: {} }); } 
    if(stack1 || stack1 === 0) { buffer += stack1; } 
    buffer += "\n </div>\n</div>\n"; 
    return buffer;}); 
      return HandlebarsTemplates["people/index"]; 
      }).call(this); 

回答

2

/assets/templates/people/index.js这混乱意味着你的车把模板被编译为JavaScript JavaScript代码前,会看见他们。

如果您说$(x).html()其中x与任何内容不匹配,您将收到null回。所以你可能根本没有#lorem2,你只需要编译模板HandlebarsTemplates["people/index"]。这意味着,你render的这一部分:

var template = Handlebars.compile($("#lorem2").html()); 

将失败,并给你TypeError例外。尝试将其替换为:

var template = HandlebarsTemplates['people/index'];