2013-05-26 154 views
0

一個未定義的模式,我有這樣的代碼:我越來越骨幹

var Day  = Backbone.Model.extend({ 
     default: { 
      service: 'None', 
      concert: 'None', 
      bible: 'None', 
      social: 'None', 
      play: 'None' 
     } 
    }), 

    Day_view = Backbone.View.extend({ 

     render: function(){ 
      this.$el.html('Service:' + this.model.service + '<br />Concert:' + this.model.concert + '<br />Bible:' + this.model.bible + '<br />Social:' + this.model.social + '<br />Play:' + this.model.play); 
      return this; 
     } 
    }), 

    day_view = new Day_view({model: new Day({service: '10', concert: '11', bible: '11', social: '13', play: '14' })}); 
    alert(day_view.render().$el.html()); 

而且我得到這個:

服務:未定義的音樂會:未定義的聖經:未定義的社會:未定義播放:未定義

問題在哪裏? 感謝

回答

1

您在骨幹訪問屬性的方式是通過get

this.model.get('service') 
+0

忘了模型(它不是JSON)......或者我可以使用model = this.model.toJSON,然後使用model.service – Symba

0

此外,因爲你沒有定義視圖的el它拋出的是未定義的錯誤。您必須將DOM元素傳遞到Backbone視圖,作爲插入html的鉤子點。

$(document).ready(function() { 
    var Day = Backbone.Model.extend({ 
     default: { 
      service: 'None', 
      concert: 'None', 
      bible: 'None', 
      social: 'None', 
      play: 'None' 
     } 
    }), 

    Day_view = Backbone.View.extend({ 
     el: 'body', 
     render: function(){ 
      this.$el.html('Service:' + this.model.get('service') + '<br />Concert:' + this.model.get('concert') + '<br />Bible:' + this.model.get('bible') + '<br />Social:' + this.model.get('social') + '<br />Play:' + this.model.get('play')); 
      return this; 
     } 
    }), 

    day_view = new Day_view({model: new Day({service: '10', concert: '11', bible: '11', social: '13', play: '14' })}); 
    alert(day_view.render().$el); 
});