François Richard
François Richard

Reputation: 7045

backbone.history.naviguate trigger router staying on the same page

My problem is pretty simple.

I start on /app, when I click on some button I trigger /report with Backbone.history.navigate('report',{trigger: true, replace: true});

Then I'm on /report page. No problem.

I have the same button on /report and I would like to trigger router route "report" when i click on it. It seems it's not working because I'm already on the asked page.

Any idea how I should proceed ?

thanks a lot

Upvotes: 0

Views: 82

Answers (1)

Balaji
Balaji

Reputation: 1019

Backbone.history.navigate('report',{trigger: true, replace: true});

You should not be using this to render/reload your application views.This is not recommended.

Rather you should have a controller object (a js object) which extends Backbone.Events

Define a callback for the for the route "/report" as follows:

var MyAppBus = _.extend({},Backbone.Events);
MyAppBus.on("report",function(payLoad){
    // 1.Load the data
    // 2.Create an instance of the view passing the loaded data.
    // (In your case, you could also pass an instance of the view in the payload 
    // and update the data alone in the view instance.)
    // 3.Render the view.
    // 4.Call the router navigate function with trigger set to false to  
    //   update the url on a need basis. 
});

Now, in the event/router callback you can do something like this:

MyAppBus.trigger("report",payLoad);

Where payLoad in the event callback could be:

var payLoad = {};
payLoad.view = viewInstance; 

Upvotes: 1

Related Questions