2012-04-23 90 views
0

我有添加第二個收藏到我的骨幹/ Rails項目骨幹和Rails的

這裏的一個問題是收集公司

class Raffler.Collections.Companies extends Backbone.Collection 
    url: '/api/companies' 
    model: Raffler.Models.Company 

這裏我的類文件是爲Model

類文件
class Raffler.Models.Company extends Backbone.Model 

繼承人路由器

​​

這裏是視圖

class Raffler.Views.CompaniesIndex extends Backbone.View 

    template: JST['companies/index'] 

    initialize: -> 
    @companies.on('reset', @render, this) 

    render: -> 
    $(@el).html(@template(companies: @companies)) 
    this 

,當我到達@companies.on它倒下 - 錯誤是「對」無法調用未定義。

我不明白這個錯誤 - 我已將@companies設置爲路由器中的集合,並將它傳遞到在路由器類中創建的視圖中。

我在應用程序中的另一個集合上實現了完全相同的代碼,所以我想知道是否因爲我試圖添加第二個集合?

這一切都完美的作品JavaScript控制檯內的瀏覽器,當我做了以下

companies = new Raffler.Collection.Companies() 
companies.fetch() 
companies.length 

我可以看到它正在調用服務器並返回正確的記錄數 - 那麼,爲什麼沒有按應用程序中的代碼工作?有任何想法嗎?

回答

3

在你的路由器,你這樣說:

view = new Raffler.Views.CompaniesIndex(collection: @companies) 

將在新CompaniesIndex的@collection property設置爲@companies

有跡象表明,如果獲得通過,將附着幾個特殊選項直接的觀點:[...] collection [...]。

所以在你看來,你應該是指@collection,不@companies

class Raffler.Views.CompaniesIndex extends Backbone.View 
    #... 
    initialize: -> 
    @collection.on('reset', @render, @) 

@companies僅在路由器內部定義,認爲只知道@collection除非你明確指定的東西@companies內視圖。

+0

Thankyou Thankyou ...這工作。謝謝。 – 2012-04-23 19:27:30