2013-03-25 74 views
1

只是想知道为什么我的路由器没有方法导航?我的new App.Router;正确调用。在我看来,我试着打电话给我的索引视图,像这样:骨干路由器没有方法导航

addTrip: function(e){ 

    e.preventDefault() ; 

    //Adding to collection 
    var newTrip = this.collection.create({ 
     title: this.$('#new_trip #title').val(), 
     where: this.$('#new_trip #where').val(), 
     desc: this.$('#new_trip #desc').val(), 
     rating: this.$('#new_trip .active').text() 
    }, { wait: true }) ; 

    if(newTrip.validationError){ 
     this.errorReport(newTrip.validationError) ; 
    } 

    this.clearForm() ; 

    //Navigate to home route 
    App.Router.navigate('', true) ; 

} 

我得到的Chrome浏览器开发工具以下错误:

Uncaught TypeError: Object function(){ return parent.apply(this, arguments); } has no method 'navigate'

我甚至试着打从控制台导航和它似乎也没有工作。

我在做什么错?

+0

你怎么声明'App.Router'? – neebz 2013-03-25 10:20:06

回答

9

您应该调用导航对象的路由器。看来你是在课堂上自己调用它。

App.myRouter = new Backbone.Router() //or if you have your own base router class App.myRouter = new App.Router() 


myRouter.navigate('', true); 
+0

我在'index.html'中声明了我的路由器,但需要在我的方法中重新声明它。谢谢@nEEbz – 2013-04-16 06:34:45

+0

使用路由器的实例我仍然无法触发.navigate(myPath)。它在我调用Backbone.history.navigate(myPath)时起作用。 – Micros 2013-07-29 13:55:38