2013-10-28 144 views
0

我想知道是怎麼開始的backkbone獲取骨幹模型的屬性

從服務器返回的在我的模型我有這個

myApp.Model = Backbone.Model.extend({ 
      urlRoot: '/items', 

      parse: function(response) { 
       response.id = response._id; 
       return response; 
      } 

}); 

在我看來,我有這樣的一個模型的屬性

initialize: function() { 
       this.model = new myApp.Model(); 
       this.model.fetch(); 
      }, 

      render: function() { 
       this.$el.html(this.template(this.model.toJSON())); 
       return this; 
      } 

這是我在我的模板

<%- this.model.attributes.age %> 

出於某種原因,在模板中執行上述操作不會導致任何輸出。

age

模板使用從服務器 enter image description here

+0

儘量只''<%= age %>。 'template'函數來自哪裏,下劃線? – numbers1311407

+0

是的,模板是從下劃線,我將編輯我的帖子,以反映, – tawheed

回答

2

你傳遞的屬性數據結構的underscorejs

形象從我找回從服務器的數據的屬性他們自己的模板功能。默認情況下,下劃線模板將使用with將函數內的範圍擴展到傳遞的對象,使對象的屬性按名稱可用。

鑑於這種情況,你會訪問age只需用:

<%= age %> 
+0

嗯,這似乎並沒有做詭計,我越來越'未捕獲ReferenceError:年齡未定義' – tawheed

+0

如果您檢查模型的屬性渲染之前,你看到「年齡」?例如'console.log(this.model.attributes)' – numbers1311407

+0

愚蠢的我!有效! – tawheed