2012-04-23 8 views
0

나는 내 백본/레일 프로젝트 여기백본 및 레일

에 두 번째 컬렉션을 추가하는 문제를 컬렉션 회사 여기

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

내 클래스 파일이 모델

의 클래스 파일입니다되어 있습니다
class Raffler.Models.Company extends Backbone.Model 

Heres는 '라우터

class Raffler.Routers.Companies extends Backbone.Router 
    routes: 
    'companies': 'index' 
    'companies/:id' : 'show' 

    initialize: -> 
    @companies = new Raffler.Collections.Companies() 
    @companies.fetch() 
    alert @companies 

    index: -> 
    view = new Raffler.Views.CompaniesIndex(collection: @companies) 
    $('#container').html(view.render().el) 
,

그리고 여기 내가 @companies.on에 도착하면 그것은 전복보기

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을 설정하고 라우터 클래스에서 만든보기로 전달했습니다.

나는 응용 프로그램의 다른 컬렉션에서 구현 된 것과 똑같은 코드를 가지고 있으므로 두 번째 컬렉션을 추가하려고하기 때문에 궁금합니다.

그것은 모든 브라우저에서 자바 스크립트 콘솔 내에서 완벽하게 작동합니다 내가 할 때 다음

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

나는 그것이 서버에 호출을하고 레코드의 정확한 수를 반환하는 것을 볼 수 있습니다 - 왜 아무튼 응용 프로그램 내에서 코드가 작동하지 않습니까? 어떤 아이디어?

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

@companies 새 CompaniesIndex의 @collection property을 설정합니다 그 : 라우터에서

답변

3

,이 말

통과하면, 첨부됩니다 몇 가지 특별한 옵션이 있습니다 보기에 직접 : [...] collection [...].

그래서보기에, 당신은 @collection를 참조해야하지 @companies :

@companies

class Raffler.Views.CompaniesIndex extends Backbone.View 
    #... 
    initialize: -> 
    @collection.on('reset', @render, @) 
은 라우터 내부에 정의 된 뷰는 약 @collection 명시 적으로 내부 @companies에 뭔가를 지정하지 않는 한 알고 전망.

+0

감사합니다 ... 감사합니다. 고맙습니다. –