2012-04-23 91 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