Магистральный маршрутизатор не работает с использованием шаблона

Хорошо, я думаю, что это что-то простое, однако я слишком глуп, чтобы это увидеть. Вот мой код в магистрали с использованием стандартного метода магистрали.

require([
  "app",

  // Libs
  "jquery",
  "backbone",

  // Modules
  "modules/example"
],

function(app, $, Backbone, Example) {

  // Defining the application router, you can attach sub routers here.
  var Router = Backbone.Router.extend({
    routes: {
      "": "index",
      "item" : 'item'
    },

    index: function() 
    {
      console.info('Index Function');
      var tutorial = new Example.Views.Tutorial();

      // Attach the tutorial to the DOM
      tutorial.$el.appendTo("#main");

      // Render the tutorial.
      tutorial.render();
    },

    item: function()
    {
        console.info('Item View');
    }
  });

  // Treat the jQuery ready function as the entry point to the application.
  // Inside this function, kick-off all initialization, everything up to this
  // point should be definitions.
  $(function() {
    // Define your master router on the application namespace and trigger all
    // navigation from this instance.
    app.router = new Router();

    // Trigger the initial route and enable HTML5 History API support
    Backbone.history.start({ pushState: true, root: '/reel' });
  });

  // All navigation that is relative should be passed through the navigate
  // method, to be processed by the router.  If the link has a data-bypass
  // attribute, bypass the delegation completely.
  $(document).on("click", "a:not([data-bypass])", function(evt) {
    // Get the anchor href and protcol
    var href = $(this).attr("href");
    var protocol = this.protocol + "//";

    // Ensure the protocol is not part of URL, meaning its relative.
    if (href && href.slice(0, protocol.length) !== protocol &&
        href.indexOf("javascript:") !== 0) {
      // Stop the default event to ensure the link will not cause a page
      // refresh.
      evt.preventDefault();

      // `Backbone.history.navigate` is sufficient for all Routers and will
      // trigger the correct events.  The Router's internal `navigate` method
      // calls this anyways.
      Backbone.history.navigate(href, true);
    }
  });

});

Я запускаю это на сервере MAMP, и когда я набираю Localhost:8888/reel , я получаю пример индексной страницы, который поставляется с шаблоном. Однако, когда я набираю Localhost:8888/reel/item или Localhost:8888/reel/#item, я получаю сообщение: страница не может быть найдена или направлена ​​обратно на мою индексную страницу.

Мой вопрос в том, что я делаю неправильно. Нужно ли использовать htaccess? Это не кажется правильным. Есть ли способ использовать магистраль для сортировки этого. Извините, если это действительно просто, просто не могу понять это.


person mcclennon19    schedule 10.07.2012    source источник


Ответы (1)


проблема может заключаться в флаге pushState.

При этом запрос проходит весь путь до сервера, и он видит полный URL-адрес и отвечает на него, что бы он ни делал...

это работает, если у вас есть

 $(function (){
       setTimeout(navMe, 2000);
 });
 function navMe() {
      backbone.navigate("item");
 }

таким образом, через 2 секунды после загрузки он перейдет к представлению элемента, и вы знаете, что это из-за того, что запрос идет на сервер, а не на магистраль.

person MarkKGreenway    schedule 11.07.2012