Skip to content

Send back target path for easier using navigo#101

Closed
martonx wants to merge 1 commit intokrasimir:masterfrom
martonx:patch-2
Closed

Send back target path for easier using navigo#101
martonx wants to merge 1 commit intokrasimir:masterfrom
martonx:patch-2

Conversation

@martonx
Copy link
Copy Markdown

@martonx martonx commented Mar 29, 2017

When we catch a navigate event, and we want to do ajax call based on the catched url, and we use just some general route like /user/:action/:id in our rooter, then we need to know what was the originally catched url, without rebuild the original url from response parameters, query strings or without any hacky other solutions). I know this is not the most elegant way, but it doesn't break anything and better then nothing. In v5 navigo should send back an object with { params, query, targetPath } instead of 3 arguments.

When we catch a navigate event, and we want to do ajax call based on the catched url, and we use just some general route like /user/:action/:id in our rooter, then we need to know what was the originally catched url, without rebuild the original url from response parameters, query strings or without any hacky other solutions). I know this is not the most elegant way, but it doesn't break anything and better then nothing. In v5 navigo should send back an object with { params, query, targetPath } instead of 3 arguments.
@krasimir
Copy link
Copy Markdown
Owner

The router already have _lastRouteResolved property that contains the latest resolved URL. I just made it available as a public method router.lastRouteResolved(). Here's a test that covers it:

describe('when we have a complex route matching', function () {
  it('should access easily the matched URL', function () {
    var handler = sinon.spy();

    router = new Navigo();
    router.on('/user/:action/:id', handler);

    router.navigate('/user/answer/42').resolve();

    expect(handler)
      .to.be.calledOnce
      .and.be.calledWith({
        action: 'answer',
        id: '42'
      }, '');
    expect(router.lastRouteResolved()).to.deep.equal({
      url: '/user/answer/42',
      query: ''
    });
  });
});

Use 4.5.0. lastRouteResolved is available there.

@krasimir krasimir closed this Mar 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants