1

我在Requirejs中使用了Backbone.js和underscore.js。但是,當我嘗試加載我的視圖模板,它給我(8超出範圍6)錯誤在Underscore.js第8行。 請告訴我我做錯了什麼。(8超出範圍6)Underscore.js模板

這裏是我的代碼:

var imageView = new ImageView({model: item}); 


define(['jquery','underscore','backbone','imageview','text!../templates/template_image.html'], 
function($, _, Backbone, ImageView, template){ 
     var ImageView = Backbone.View.extend({ 
      initialize: function(){ 
       this.showImageTemplate = _.template(template);    
      }, 
      render: function(){ 
       var html = this.showImageTemplate(this.model); 
       this.$el.html(html); 
       return this; 
      } 
     }); 
    return ImageView; 
}); 

我的模板文件:

<img id="frameImg" src="<%= DocumentPath %>/<%= DocumentName %>" alt="image" title="image"/> 
+0

此外,當我嘗試CONSOLE.LOG(this.model)的渲染功能,它讓我看到我的完整的模型,但我得到的錯誤,當我運行我的代碼: 的ReferenceError:DocumentPath沒有定義(8超出範圍6) PS。 DocumentPath包含在型號 – xTMNTxRaphaelx

回答

1

你傳遞的原始Backbone.Model對象作爲數據到您的模板,所以你喜歡的東西的工作

{  
    _changing: false, 
    _pending: false, 
    _previousAttributes: {} 
    attributes: { 
     DocumentPath: "", 
     DocumentName: "" 
    } 
    ... 
} 

您可能只想要屬性,您可以通過例如,。嘗試:

var html = this.showImageTemplate(this.model.toJSON()); 
+0

謝謝,這是這個問題。我以前沒有意識到我的錯誤。 現在正在工作,再次感謝:) – xTMNTxRaphaelx