A
A
Alexey Ermolaev2017-05-27 14:17:42
JavaScript
Alexey Ermolaev, 2017-05-27 14:17:42

How to make friends between Backbone Router and Webpack-dev-server?

I'm trying to set up Backbone Router to process urls without a hash after the slash
eg (` localhost:9500/photos/1234 ` instead of ` localhost:9500/#photos/1234 `).
In the main file I create a new router instance and call ` Backbone.history.start({ pushState: true }) `.
Unfortunately, when you click on the link, the page keeps reloading, and of course, a 404 crashes. The ` historyApiFallback ` option in the dev server config does no good, it only redirects to the root path.
That is, my urls are not intercepted by the router. If there is a hash in the url, then it is processed, otherwise not. How can this be set up properly?
Yes, I'm using handlebars for templating, and the photo view template looks something like this:

<article class="photo__item">

    <a href="photos/{{ id }}">

      {{ id }} <br>
      {{ date }} <br>

      <img src="{{ baseImage }}" alt="">

      <p>{{ text }}</p>

    </a>
  </article>

The dev-servrea config looks like this:
devServer: {
    contentBase: path.join(__dirname, "./server"),
    publicPath: "/public/",

    historyApiFallback: {
      index: "/"
    },

    compress: true,
    hot: true,

    port: 9500
  },

Route to detailed photo in router class
get routes() {

    return {
      "photos/:id": "getDetail"
    };

  }

  //Пока хотя бы записать в консоль
  getDetail(query) {
    console.log(query);
  }

And calling the main view:
$(() => {

  moment.locale("ru");

  VK.init({ apiId });

  const mainView = new MainView();

  rootNode.append(
    mainView.render().$el);

  const router = new Router();

  Backbone.history.start({ pushState: true });

});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question