2012-06-26 8 views
1

레일을 사용하고 있습니다. 방금 backbone-rails 보석을 설치하고 here 지침을 따랐습니다. 내가 Backbone.history.start()을 할 때Uncaught TypeError : Backbone.history.start()에서 'start'메서드를 undefined로 호출 할 수 없습니다.

, 나는이 오류가 :

Uncaught TypeError: Cannot call method 'start' of undefined 

내가 Backbone.history.start()는 당신이 this other SO question에 따라, 적어도 하나 개의 경로를 생성 할 때까지 호출 할 수 없습니다 것을 이해합니다.

class Lunchhub.Routers.RestaurantLocationsRouter extends Backbone.Router 
    initialize: (options) -> 
    @restaurantLocations = new Lunchhub.Collections.RestaurantLocationsCollection() 
    @restaurantLocations.reset options.restaurantLocations 

    routes: 
    "new"  : "newRestaurantLocation" 
    "index" : "index" 
    ":id/edit" : "edit" 
    ":id"  : "show" 
    ".*"  : "index" 

가 왜이 오류가 점점 오전 : 그게 내가이 커피 스크립트 파일에 정의 된 여러 경로를 가지고 할 수 있기 때문에,하지만, 내 문제가 될 것 같지 않습니다?

편집 :

<div id="restaurant_locations"></div> 

<script type="text/javascript"> 
    $(function() { 
    window.router = new Lunchhub.Routers.RestaurantLocationsRouter({restaurantLocations: <%= @restaurant_locations.to_json.html_safe -%>}); 
    Backbone.history.start(); 
    }); 
</script> 
+1

'Backbone.history.start() '를 호출하기 전에 어떤 경로의 * 인스턴스 *를 만드시겠습니까? – jackwanders

+0

http://jsfiddle.net/ambiguous/a5SLF/와 http://jsfiddle.net/ambiguous/a5SLF/1/의 동작을 비교하면, 두 번째 것은 '새로운 R'이고 첫 번째는 그렇지 않습니다. –

+0

나는 생각한다. 내 코드를 포함하도록 수정했습니다. –

답변

1

Backbone.history.start를 호출하기 전에 새로운 키워드로 라우터의 인스턴스를 생성 해보십시오 : 여기 내 코드입니다.

EDIT : Backbone.history는 undefined이어야하며 다른 사람들은 일반적으로 there needs to be a router이며 Backbone이이 개체를 만들기 전에 하나 이상의 경로가 활성화되어 있기 때문입니다.

Backbone.js creates an instance of Backbone.History (upper case ‘H’) called Backbone.history (lower case ‘h’) once a controller has been created that has at least one route specified on it

난 당신이 라우터의 새로운 인스턴스를 생성하고 볼 수 있지만 그것은 나쁜 라우터 인스턴스가 여전히 문제, 기타 문제에서 논의보다는 단지 다른 맛입니다 가능 - typeof 당신의 window.router 객체 라우터를 확인 Backbone.history.start() 전화가 오기 전에 당신이 기대하는 것을 돌려 주나요?

편집 : 아래의 댓글에서 확인 된대로 RestaurantLocationsRouter은 다소 나쁩니다. 우리는 아직 그 이유를 알아 내지 못했지만 새 라우터를 만들면 예상대로 작동하고 실제로 라우터 인스턴스에 문제가 있음을 나타냅니다.

+0

나는 이미 그렇게하고 있다고 생각하지만 잘못 될 수 있습니다. 내 코드를 포함하도록 수정했습니다. –

+0

window.router 객체의 'typeof'를 확인하여 반환되는 내용을 확인할 수 있습니까? 실제로 이것이 백본의 인스턴스가 아닌 것일 수 있습니다. – buley

+0

마치 'RestaurantLocationsRouter'의 인스턴스처럼 보입니다. 'typeof'는'object'입니다. –

관련 문제