2013-07-30 89 views
0

我有一個呈現骨幹視圖的路由器。 (我用requirejs也)骨幹視圖中的未定義集合問題

initialize: function() { 
     var crateCollection = new CrateCollection(); 
     crateCollection.fetch({ 
      success: function() 
      { 
       var cratesView = new CratesView({ model: crateCollection }); 
      } 
     }); 
    } 

在視圖的初始化集合是所有罰款,我可以輸出到CONSOLE.LOG,並認爲它是適當的填充。

initialize: function (models) { 
     var crateCollection = models.model; 
     console.log(crateCollection); 
     this.render(); 
    } 

然而,當我運行呈現功能crateCollection是不確定的,並與JS錯誤的「無法訪問的不確定模型」

render: function() { 
     $(this.el).html(this.template()); 
     var that = this; 
     console.log(this.crateCollection); 
     _.each(this.crateCollection.models, function (item) { 
      that.renderCrate(item); 
     }, this); 
    }, 

爲什麼這個集合無法從這個訪問功能?當它在初始化視圖之前等待異步調用返回成功時,它肯定應該被填充,所以它們不應該存在任何問題。

我錯過了什麼?

回答

2

initializeView使用

this.crateCollection = models.model; 

,而不是

var crateCollection = models.model; 

我認爲是一個crateCollection範圍的問題。

+0

謝謝!我知道這是一個範圍問題,但無法解決原因。 ;-) –

+0

很高興幫助你:) – muneebShabbir