Skip to content
This repository

Add the ability to disable navigate same url short circuit #1214

Closed
markdaws opened this Issue April 11, 2012 · 9 comments

9 participants

Mark Dawson Jeremy Ashkenas Ken Perkins Aaron Surty tspike Cristian José Magalhães Ulrira twler
Mark Dawson

In the navigate method of the Router object, there is the following line:

if (this.fragment == frag) return;

So if the target URL is the same as the current URL, the navigate method does nothing. In my app I want to be able to execute the routing code even if the url is the same.

I'm thinking that an extra field could be passed to the options object of the navigate method:

force: [true|false]

by default this is set to false, but if set to true the navigate method would still run even if the current url is the same as the target url. Has this been discussed before? I am willing to do a pull request with the change if it would be accepted to the project.

Thanks
Mark.

Jeremy Ashkenas
Owner

Yep -- it's been discussed a bunch of times before. Instead of calling .navigate, simply call the function you wanted to call in the first place. In fact, the trigger: true option to navigate is often a code smell.

Jeremy Ashkenas jashkenas closed this April 11, 2012
Ken Perkins

Here's the problem I have with this closing this bug:

A key point of using backbone is to handle parsing of routes, and then calling a function based on the route. Lacking the ability to determine cases where a "reload" is ok through backbone directly, we have to do something to this effect:

        if (url == window.location.pathname + window.location.search) {
            Backbone.history.loadUrl(url);
            return false;
        }
        else if (app.router.isHandled(url)) {
            Backbone.history.navigate(url, {
                trigger: true,
                forceReload: true
            });
            return false;
        }

Does this work? yea, but it does feel like this is a totally valid use case that would be nice to have in the platform.

Aaron Surty

Are there any articles you can point me towards on why using "trigger:true" is code smell?

thanks

Jeremy Ashkenas
Owner

There aren't any articles ... but because it forces you to serialize your application state through a string (a URL, to be precise), when you don't need to. Just calling a JavaScript function is a much richer API ... you can pass objects, references, and you don't lose all of your external state.

Think of Backbone's changing the URL in the browser as dropping a bookmark that folks can go back to later -- not as a thing that you need to do to change your application's state.

Cristian

... sorry guys, but I could not figure out why a "forceFragment" capability is seen such an antipattern. I can only see a flexibility gain here.

José Magalhães

Though I totally agree with @jashkenas reply on the top of the thread, I kind of agree more with @dotBits opinion. Backbone is great for not enforcing any patterns, as a developer it is expected that navigate + {trigger: true} will always fire any bound methods and route events, even if navigating to the same URL as before.

Ulrira

Same need here.

I have a table with pagination and I'm deleting a row. After that, I want to reload the same #url to reload the current page of the table.

If the deleted row is the last, I want to reload the same url but changing the page to page-1

twler
twler commented April 16, 2014

+1 for same need case as @Ulrira

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.