javascript - 'undefined' is not a function (evaluating 'Backbone.$(window).on('hashchange', this.checkUrl)') -


i'm trying configure router backbone.js , receiving exception @ line 1414 of backbone.js 1.1.2

the error exists safari , chrome:

[error] typeerror: 'undefined' not function (evaluating 'backbone.$(window).on('hashchange', this.checkurl)') start (backbone.js, line 1414) (anonymous function) (index.html, line 27) 

here's index.html file, line 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> 

i can't find many resources out, , of pre-existing resources emphasize allocation of router, , feel it's implemented correctly. also, don't think it's necessary have collection or model since i'm trying implement routing @ time. appreciated, thank you.

** edit **

i reordered script imports to:

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

and error changed typeerror: 'undefined' not function (evaluating 'backbone.$(window)')

adding breakpoint line 1414 of backbone.js indicates error evaluating this.checkurl

 checkurl: function () { [native code] } arguments: (...) arguments: function throwtypeerror() { [native code] } set arguments: function throwtypeerror() { [native code] } caller: (...) 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] 

the way set router de facto according multiple sources, irrespective of (function($){})(jquery) , dunno how path assigned correctly. using # did not me.

i notice 2 things in code:

  1. the routes object supposed map route patterns method names (or anonymous functions):

    the routes hash maps urls parameters functions on router (or direct function definitions, if prefer)

    but don't have index method defined in pagerouter.

  2. if @ route docs (or catalogue of events), see that:

    the name argument triggered "route:name" event whenever route matched.

    so router trigger 'route:index' events, not 'index' events.

i can't reproduce error you're seeing if address both of points, things seem working expected:

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(); 

demo: http://jsfiddle.net/ambiguous/kt7l8/


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -