本文介绍了主干路线不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我一直无法弄清楚的呢。这很简单,这可能是为什么我在与它:)麻烦

I have a strange issue I haven't been able to figure out as of yet. It's very simple which is probably why I'm having trouble with it :)

首先,这里的路由表...

First, here's the routing table...

routes: {
    '': 'root', //called
    'report': 'report', // called
    'report/add': 'reportAdd', // not called
    'report/print': 'reportPrint', // not called
    'report/settings': 'reportSettings', // not called
},

您会看到我标记哪些是工作,哪些不是。这个问题归结为所有的子路径(即报告/添加)没有被匹配。

You'll see I marked which ones are working and which ones aren't. The problem boils down to all subroutes (i.e report/add) not being matched.

骨干的历史正确地称为main.js像这样:

Backbone history is called properly in main.js like so:

app.Router = new Router();
Backbone.history.start({ pushState: true });

显然,这是在正确的位置,因为路由工作只是没有子路由。我已经试过无声参数 Backbone.history 选项和所有没有任何运气。

Obviously, that's in the right spot because routes are working just not sub-routes. I've tried the root options of Backbone.history and the silent parameter all without any luck.

我想这是一个配置/设置问题,但我一直没能找到任何答案。我究竟做错了什么?任何帮助深表AP preciated。

I imagine it's a configuration/setup issue but I haven't been able to find any answers. What am I doing wrong? Any help is much appreciated.

顺便说一句,我使用requirejs和骨干样板,但我看不出这将有所作为。

Btw, I'm using requirejs and Backbone Boilerplate but I don't see how that would make a difference.

更新:虽然提供的答案是技术上是正确的,问题是骨干样板。看到这个的一个底部说明。我有作为第一个评论者有同样的问题。

UPDATE: Although the answer provided is technically correct, the problem is with Backbone Boilerplate. See the bottom of this blog post for an explanation. I'm having the same issue as the first commenter there.

推荐答案

由于在评论中讨论的,问题在于,用推状态风格的URL时,服务器无法识别主干航线的URL。

As discussed in the comments, the problem is that, when using push-state style URLs, the server doesn't recognize the Backbone route URLs.

有关说明,说你的应用程序的根是服务器/应用/ index.html的,而你正在尝试使用的URL骨干航线 /报告/打印。随着网址片段路由,这是好的:

For illustration, say your application's root is at server/app/index.html, and you're trying to use a URL that Backbone routes to /report/print. With URL fragment routing, this is fine:

http://server/app/index.html#report/print

服务器后并返回的index.html忽略的部分;然后负载主干航线报告/打印

The server ignores the part after # and returns index.html; then on load Backbone routes to report/print.

但是,如果你使用的推状态路由,然后在URL看起来是这样的:

But if you're using push-state routing, then the URL looks like this:

http://server/app/index.html/report/print

和服务器抛出一个404错误,因为它不承认在这条道路什么,所以骨干,是从来没有加载。

And the server throws a 404 error because it doesn't recognize anything at that path, so Backbone is never even loaded.

的解决方案是:


  1. 作为注意到,修改服务器code,这样服务器呈现每个骨干正确的内容路由或

  2. (我认为这是比较容易)把一个URL重写来代替Web服务器上(的,),使之返回 index.html的对于像 ,<$ C的index.html /报告/打印一个主干航线的任何请求$ C>的index.html /报告/添加等

  1. As the Backbone.js docs note, modify server code, so that the server renders the correct content for each Backbone route, or
  2. (which I think is easier) put a URL rewrite in place on the web server (IIS, Apache), so that it will return index.html for any request that is a Backbone route like index.html/report/print, index.html/report/add, etc.

在IIS中,例如,你把在web.config以下应用程序根目录下:

In IIS, for example, you'd put the following in the web.config under your application root:

<rewriteMaps>
    <rewriteMap name="StaticRewrites">
        <add key="index.html/report/print" value="index.html" />
        <add key="index.html/report/add" value="index.html" />
        <!-- etc -->
    </rewriteMap>
</rewriteMaps>

这篇关于主干路线不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:49