未处理的路线

发布于 2025-01-06 04:11:17 字数 1209 浏览 2 评论 0 原文

我有一个node.js(服务器)和backbone.js(客户端)应用程序 - 我可以在页面上加载并初始化我的主干应用程序...并初始化路由器,但我的默认路由(“.*”)是没有被叫到。我可以在初始化路由器后手动调用索引函数,但当我通过 Rails 构建骨干应用程序时,我不必执行该步骤。

有谁知道为什么会发生这种情况?

添加代码(在咖啡脚本中):

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '.*'        : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @registry_patients = new NodeNetBackbone.Collections.RegistryPatients()
    # TODO: Figure out why this isn't sticking...
    @registry_patients.model = NodeNetBackbone.Models.RegistryPatient
    # TODO: Try to only round trip once on initial load
    # @registry_patients.reset($('#container_data').attr('data'))
    @registry_patients.fetch()

    # TODO: SSI - why are the routes not getting processed?
    this.index()

  index: ->
    console.log 'made it to the route index'
    view = new NodeNetBackbone.Views.RegistryPatients.Index(collection: @registry_patients)
    # $('#container').html('<h1>Patients V3: (Backbone):</h1>')
    $('#container').html(view.render().el)

I've got a node.js (server) and backbone.js (client) app - I can load and init my backbone app on a page... and init the router, but my default route (".*") is not getting called. I can manually call the index function after I initialize the router, but I don't have to take that step when I've built backbone apps over rails.

Does anyone have a clue as to why this is happening?

adding code (in coffeescript):

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '.*'        : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @registry_patients = new NodeNetBackbone.Collections.RegistryPatients()
    # TODO: Figure out why this isn't sticking...
    @registry_patients.model = NodeNetBackbone.Models.RegistryPatient
    # TODO: Try to only round trip once on initial load
    # @registry_patients.reset($('#container_data').attr('data'))
    @registry_patients.fetch()

    # TODO: SSI - why are the routes not getting processed?
    this.index()

  index: ->
    console.log 'made it to the route index'
    view = new NodeNetBackbone.Views.RegistryPatients.Index(collection: @registry_patients)
    # $('#container').html('<h1>Patients V3: (Backbone):</h1>')
    $('#container').html(view.render().el)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2025-01-13 04:11:17

主干路由不是正则表达式(除非您使用 route 手动添加正则表达式路由)。来自精细手册

路由可以包含参数部分,:param,它匹配斜杠之间的单个 URL 组件;和 splat 部分*splat,它可以匹配任意数量的 URL 组件。

[...] "file/*path" 的路由将匹配 #file/nested/folder/file.txt,传递 "nested /folder/file.txt” 到操作。

如果我们检查源代码,我们会看到这个

// Backbone.Router
// -------------------
//...
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var namedParam    = /:\w+/g;
var splatParam    = /\*\w+/g;

因此,您的 '.*' 路由应该只匹配文字 '.*' 而不是您期望的匹配“任何内容”。

我认为你想要更像这样的东西:

routes:
  ''          : 'index'
  #...
  '*path'     : 'index'

确保你的 *path 路线位于 路线列表的底部

// Bind all defined routes to `Backbone.history`. We have to reverse the
// order of the routes here to support behavior where the most general
// routes can be defined at the bottom of the route map.

这种关于对象中元素“顺序”的假设对我来说似乎相当危险且考虑不周,因为 没有保证顺序

未指定枚举属性的机制和顺序(第一个算法中的步骤 6.a,第二个算法中的步骤 7.a)。

我认为你最好在 initialize 方法中手动添加默认的 *path 路由:

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @route '*path', 'index'
    #...

Backbone routes are not regexes (unless you manually add a regex route using route). From the fine manual:

Routes can contain parameter parts, :param, which match a single URL component between slashes; and splat parts *splat, which can match any number of URL components.

[...] A route of "file/*path" will match #file/nested/folder/file.txt, passing "nested/folder/file.txt" to the action.

And if we check the source, we'll see this:

// Backbone.Router
// -------------------
//...
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var namedParam    = /:\w+/g;
var splatParam    = /\*\w+/g;

So your '.*' route should only match a literal '.*' rather than matching "anything" as you're expecting.

I think you want something more like this:

routes:
  ''          : 'index'
  #...
  '*path'     : 'index'

Make sure your *path route is at the bottom of your route list:

// Bind all defined routes to `Backbone.history`. We have to reverse the
// order of the routes here to support behavior where the most general
// routes can be defined at the bottom of the route map.

This assumption about the "order" of elements in an object seems rather dangerous and ill-considered to me as there is no guaranteed order:

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

I think you'd be better off adding your default *path route manually in your initialize method:

class NodeNetBackbone.Routers.RegistryPatients extends Backbone.Router
  routes:
    ''          : 'index'
    '/index'    : 'index'
    '/:id'      : 'show'
    '/new'      : 'new'
    '/:id/edit' : 'edit'

  initialize: ->
    console.log 'init the router'
    @route '*path', 'index'
    #...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文