3

好的,我认为这很简单,但是我很愚蠢地看到它。这是我在主干中使用主干样板方法的代码

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 吗?这似乎不对。有没有办法使用主干对此进行排序。对不起,如果这真的很简单,只是无法理解它。

4

1 回答 1

1

问题可能在于 pushState 标志。

随着请求一直到服务器,它会看到完整的 url 并用它会做的任何事情来响应它......

如果你有一个它有用吗

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

这样加载后 2 秒,它将导航到项目视图,您知道这是因为请求进入服务器而不是主干。

于 2012-07-11T05:42:17.970 回答