2012-07-24 198 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'];