2014-04-19 30 views
1

我想配置路由器與Backbone.js和接收異常在行1414 Backbone.js的的Safari瀏覽器和Chrome存在1.1.2

錯誤:

[Error] TypeError: 'undefined' is not a function (evaluating 'Backbone.$(window).on('hashchange', this.checkUrl)') 
start (backbone.js, line 1414) 
(anonymous function) (index.html, line 27) 

這裏的index.html文件,第27行是Backbone.History.start()

<!DOCTYPE html> 
<html lang="en"> 
<meta charset="utf-8"> 
<head> 
    <link rel="stylesheet" href="bootstrap.min.css"> 
</head> 
<body> 
    <div class="container"> 
    </div> 
    <script src="../jquery.js"></script> 
    <script src="../underscore.js"></script> 
    <script src="../backbone.js"></script> 
    <script src="../json2.js"></script> 

</body> 

<!-- BACKBONE --> 
<script type="text/javascript"> 
    (function($) { 
     var PageRouter = Backbone.Router.extend({ 
      routes: { 
       '': 'index' 
      } 
     }); 
     var pageRouter = new PageRouter(); 
     pageRouter.on('index', function() { 
      console.log('hey'); 
     }); 
     Backbone.history.start(); 
    })(jQuery) 
</script> 
</html> 

我找不到很多資源來幫忙,並且大部分的公關現有的資源強調了路由器的分配,並且我感覺它的實現是正確的。此外,我不認爲有必要有一個集合或模型,因爲我只是試圖在這個時候實現路由。任何幫助將不勝感激,謝謝。

**編輯**

我重新排序腳本進口:

<script src="../underscore.js"></script> 
<script src="../backbone.js"></script> 
<script src="../jquery.js"></script> 
<script src="../json2.js"></script> 

,改變了錯誤TypeError: 'undefined' is not a function (evaluating 'Backbone.$(window)')

添加斷點Backbone.js的行1414指示錯誤來自評估this.checkUrl

 
checkUrl: function() { [native code] } 
arguments: (...) 
get arguments: function ThrowTypeError() { [native code] } 
set arguments: function ThrowTypeError() { [native code] } 
caller: (...) 
get caller: function ThrowTypeError() { [native code] } 
set caller: function ThrowTypeError() { [native code] } 
length: 1 
name: "" 
__proto__: function Empty() {} 
[[TargetFunction]]: function (e) { 
[[BoundThis]]: Backbone.History 
[[BoundArgs]]: Array[0] 

根據多個來源,我設置路由器的方式實際上是事實上的,不管(function($){})(jQuery)是什麼,也不知道如何獲得正確分配的路徑。使用#沒有幫助我。

+0

怎麼樣在該代碼中放置一個斷點? –

+0

backbone.js顯示:本地{docMode:undefined,fragment:「」,frame:undefined,loc:undefined,oldIE:null,options:undefined,this:Backbone。歷史{_hasPushState:false,_wantsHashChange:true,_wantsPushState:false}} – aug2uag

+0

嘗試傳遞選項{pushState:true},並在'extend'中定義'index:function(){}',所有上述結果都帶有'Uncaught TypeError:未定義是不是一個函數',並找不到解決方案從以前的帖子http://stackoverflow.com/questions/7343357/cannot-call-start-of-undefined-when-starting-backbone-js-history – aug2uag

回答

2

我注意到在你的代碼的兩件事情:

  1. routes object應該映射路由模式到方法名(或匿名函數):

    The routes hash maps URLs with parameters to functions on your router (or just direct function definitions, if you prefer)

    ,但你沒有一個index方法定義在PageRouter

  2. 如果你看一下route docs(或Catalogue of Events),你會發現:

    The name argument will be triggered as a "route:name" event whenever the route is matched.

    所以你的路由器會觸發'route:index'事件,不'index'事件。

我無法重現你看到但錯誤,如果我解決這兩個點,事情似乎按預期工作:

var PageRouter = Backbone.Router.extend({ 
    routes: { 
     '': 'index' 
    }, 
    index: function() { 
     console.log('index'); 
    } 
}); 
var pageRouter = new PageRouter(); 
pageRouter.on('route:index', function() { 
    console.log('hey'); 
}); 
Backbone.history.start(); 

演示:http://jsfiddle.net/ambiguous/kt7L8/

+0

感謝您糾正我的代碼,並且確實驗證源代碼適合您,以及類型和慷慨的迴應。仍然存在相同的問題,並且我得到相同的錯誤,並且只有重新排列導入源腳本的順序時,錯誤纔會更改。 – aug2uag

+0

有沒有可能在jsfiddle.net或jsbin.com上重現問題?如果我能看到它發生,可能更容易發現。 –

+0

是的,該文件工作在jsfiddle,可以在http://jsfiddle.net/aug2uag/cgwGr/ – aug2uag

相關問題