0

When I call Backbone.history.navigate, I want to be able to change a global variable.

I want to set

window.linkclicked = true; // when someone clicks a link

and

window.linkclicked = false; // when back button is pushed.

Is there a way to do this using javascript prototypes? How do I insert that logic inside the "navigate" method?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

1

You can extend the Backbone.history instance and just redefine the navigate function as you wish.

var originalNavigate = Backbone.history.navigate;

_.extend(Backbone.history, {
    navigate: function(fragment, options) {
        // do stuff before
        var returnValue = originalNavigate.apply(this, arguments);
        // do stuff after
        return returnValue;
    },
});

Or with the closure module pattern:

Backbone.history.navigate = (function(navigate) {
    return function(fragment, options) {
        /* ...snip ...*/
    }
})(Backbone.history.navigate);
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
-1

You can extend the functionality with underscore:

_.extend(Backbone.history.navigate, {
linkClicked : function( bool ){
//handle link clicked
}
});

You can call this with:

Backbone.history.navigate.linkClicked( true ); 
//or
Backbone.history.navigate.linkClicked( false );
jimmy jansen
  • 647
  • 4
  • 12