2012-04-10 21 views
2

這使我瘋了。我不能讓一個簡單的路由器工作...麻煩得到Backbone.Router使用咖啡腳本

jQuery -> 
     class MyRouter extends Backbone.Router 
      routes: 
       ""   :"index" 
       "/list"  :"showList" 
       "/item/:id" :"showItem" 
      index: => 
       alert "index" 
      showList: => 
       alert "get the lists" 
      showItem: (id)=> 
       alert "the item #{id}" 

     @app = window ? {} 
     @app = window.app ? {} 
     @app.myRouter = MyRouter 
     Backbone.history.start() 

我總是得到這樣的錯誤: index.js:50Uncaught TypeError: Cannot call method 'start' of undefined

,我看到了這一點:Cannot call 'start' of undefined when starting backbone.js history.

,但它並沒有幫助=(...

我相信這是一個簡單的人,但我是某種卡住這裏... 請幫助...

回答

6

您沒有創建Backbone.Router的實例,因此Backbone.history.start()將失敗。

@app.myRouter = new MyRouter() 

您發佈的鏈接告訴你到底是什麼問題:

TypeError: Cannot call method 'start' of undefined**

Hmm, for some reason Backbone.history is undefined, and so there is no start method on it. It turns out that 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. This makes sense, as history management is only required if there are routes to respond to.

+0

的感謝!你救了我的一天! – silverfighter 2012-04-10 19:38:06

+0

@silverfighter我讀過你鏈接的確切文章,試圖弄清楚爲什麼我的路由器不能在前一天工作。 :) – asawyer 2012-04-10 19:40:47