Backbone Router has no method navigate

2019-06-05 22:48发布

问题:

Just wondering why my router has no method navigate?

I have new App.Router; called properly. And in my view I try to call my index view like so:

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

}

I get the following error in Chrome Dev Tools:

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

I even tried to call navigate from the console and it doesnt seem to work either.

What am I doing wrong?

回答1:

You should call navigate on your router object. It seems you are calling it on the class itself.

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


myRouter.navigate('', true);