needhelpwithR
needhelpwithR

Reputation: 283

Ember.js: What file to edit to append forward slash to every route

I'm creating a basic Ember application and I want a forward slash / to be appended to the end of every URL. This is so I can use anchor links without the entire page reloading once.

For example, if I go to localhost:4200/posts and have an anchor link to /posts/#top, the first time the page will reload because the original URL is /posts and not /posts/.

Now I already know how to append a forward slash to the end of every URL using Javascript, but my question is how to do it with Ember. Is there a file where I can define this rule for every route in my application? Or would I have to define these rules for every single route I want to add a forward slash?

Upvotes: 2

Views: 291

Answers (1)

Steve H.
Steve H.

Reputation: 6947

Creation of URLs is the responsibility of the Location API. By default, you can specify what implementation of the Location API you want to use via the location property in the Router. You can create your own Location provider and register it and then specify that one in your Router. This is not something I have ever tried to do, but you may be able to extend one of the existing implementations (e.g. hash or history).

See the details of implementing the Location API here: https://github.com/emberjs/ember.js/blob/v2.7.0/packages/ember-routing/lib/location/api.js

Then, in some initializer, you should be able to do:

import CustomLocation from 'my/custom/location/api/implementation.js';
import Router from 'router';

Router.reopen({
    location: 'customLocation',
    customLocation: CustomLocation
});

Upvotes: 2

Related Questions