Trying to get the simplest backbone router to work to see if I like how it works. I am able to get models to work so I think it’s loading correctly but when I access with http://localhost:3000/xxx#testindex, nothing happens.
edit 1
I thought there wasn’t an error but now I’m getting h is not a function. Hmm… I am a little stumped.
What am I doing wrong? Thx
<html>
<head>
<script src='/assets/underscore.js'></script>
<script src='/assets/backbone.js'></script>
</head>
<body>
<script>
var PageRouter=Backbone.Router.extend({
routes:{
"testindex": "index"
},
index: function(){ alert("I am here in index")}
});
window.onload=function(){
// alert("this loaded");
var app_router = new PageRouter;
Backbone.history.start();
}
</script>
<a href="#testindex">Activate route</a>
</body>
</html>
Backbone depends on jQuery or Zepto:
Emphasis mine. You forgot to include jQuery or Zepto and that’s where your “h is not a function” error comes from. Since you’ll probably be pulling in jQuery, you should use
$(function() { ... })instead of the old schoolwindow.onload.Once you sort that stuff out, it works fine: http://jsfiddle.net/ambiguous/3XUxS/