2016-06-10 108 views
1

我想建立微觀歷史與此:BackboneJS:router.navigate不是一個函數

var router = Backbone.Router.extend({ 
    routes:{"":"home",":slug":"pages"}, 
    pages:function(slug){alert(slug);}, 
    home:function(){alert('home');} 
}) ; 

Backbone.history.start({ 
    pushState: true, 
    silent: true 
}); 

$(document).on("click","a",function(e) { 
    e.preventDefault() ; 
    router.navigate($(this).attr('href'),true) ; 
}); 

但是控制檯說:「遺漏的類型錯誤:router.navigate是不是一個函數」

我在那裏錯過了什麼?

回答

4

您需要實例路由器:

var AppRouter = Backbone.Router.extend({ 
    routes:{"":"home",":slug":"pages"}, 
    pages:function(slug){alert(slug);}, 
    home:function(){alert('home');} 
}); 

Backbone.history.start({ 
    pushState: true, 
    silent: true 
}); 

var router = new AppRouter(); 
$(document).on("click","a",function(e) { 
    e.preventDefault() ; 
    router.navigate($(this).attr('href'),true) ; 
});