0

"rails-backbone"보석으로 백본을 사용하려고하는 레일 앱 중 하나에서 그리고 잘 작동하는 스캐 폴딩을 사용하여 하나의 모델을 만들었습니다. 하지만 다른 모델을 가지고 있는데 다른 라우터를 사용하려고하지만 index.html.erb에서 해당 라우터를 인스턴스화하려고하면 해당 라우터가 실행됩니다. "Uncaught TypeError : 정의되지 않은 함수는 없습니다" 분명히 의미합니다 그런 라우터가 없다. 하지만 개발자 도구에도 JS 파일이 표시됩니다. 나는 모든 다른 방법을 시도했지만 효과가 없었습니다. 미리 감사드립니다.Backbone With Reails 3.x, 여러 라우터 용

답변

1

난 당신이 이런 식으로 라우터를 정의하고 있다는 추측에는 요 :

class SomeRouter extends Backbone.Router 
    # router code goes here 

을 한 다음 하나 만들려고 :

r = new SomeRouter 

그러나 커피 스크립트가 마무리됩니다 파일을 function to prevent scope creep :

Although suppressed within this documentation for clarity, all CoffeeScript output is wrapped in an anonymous function: (function(){ ... })(); This safety wrapper, combined with the automatic generation of the var keyword, make it exceedingly difficult to pollute the global namespace by accident.

If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. The existential operator (covered below), gives you a reliable way to figure out where to add them; if you're targeting both CommonJS and the browser: exports ? this

래퍼가 FUNC 내부 SomeRouter을 숨길 것이다 따라서 파일을 정의하는 파일 바깥에 SomeRouter이 보이지 않습니다.

Rails/Backbone 앱의 일반적인 해결책은 네임 스페이스를 직접 관리하는 것입니다. 다른 (자바가 | 커피) 전에 어딘가에 자신의 네임 스페이스를 설정 스크립트가 당겨됩니다

# AppName is just a placeholder, you'd use something more 
# sensible in real life. 
window.AppName = 
    Routers:  { } 
    Views:  { } 
    Models:  { } 
    Collections: { } 

다음 라우터를 정의로 : 나중에

class AppName.Routers.SomeRouter extends Backbone.Router 
    #... 

과 :

r = new AppName.Routers.SomeRouter 

은 모델, 컬렉션 및 뷰를 전역 적으로 볼 수 있어야합니다.

+0

감사합니다. –