【问题标题】:Backbone.Marionette: Router Not Routing CorrectlyBackbone.Marionette:路由器未正确路由
【发布时间】:2013-06-29 13:34:50
【问题描述】:

所以我在我的登陆页面视图中有一个按钮,当点击它时,应该将我的用户路由到注册 URL,在我的路由器中启动我的 register 函数,并呈现注册视图。唯一的问题是它似乎路由不正确。

这是我的登陆页面视图:

template = require './templates/landing'
app = require '../application'

module.exports = class LandingView extends Backbone.Marionette.ItemView
    id: 'landing-view'
    template: template

    events:
        'click .splash .get-started': 'route_register'

    route_register: (e) ->
        e.preventDefault()
        console.log 'button clicked'
        app.router.navigate('/register', {trigger: true})

这是我的路由器:

application = require('application')
HomeView = require('views/HomeView')
LandingView = require('views/LandingView')
RegisterView = require('views/RegisterView')

module.exports = class Router extends Backbone.Router

    routes:
        '': 'landing'
        '/home': 'home'
        '/register': 'register'

    landing: =>
        console.log 'in router: landing'
        lv = new LandingView()
        application.layout.content.show(lv)

    home: =>
        console.log 'in router: home'
        hv = new HomeView()
        application.layout.content.show(hv)

    register: =>
        console.log 'in router: register'
        rv = new RegisterView()
        application.layout.content.show(rv)

在我的登陆页面视图中,当单击按钮时,我会在 Chrome 工具中获得 console.log。但我从来没有从路由器得到 console.logs。这让我相信我的路由器被错误地实例化了,但我是这样做的:

class Application extends Backbone.Marionette.Application
    initialize: =>

        @on("initialize:after", (options) =>
            Backbone.history.start();
            # Freeze the object
            Object.freeze? this
        )

        @addInitializer( (options) =>
            # Instantiate the main layout
            AppLayout = require 'views/AppLayout'
            @layout = new AppLayout()
            @layout.render()
        )

        @addInitializer((options) =>
            # Instantiate the router
            Router = require 'lib/router'
            @router = new Router()
        )

此外,URL 栏中的地址也会发生变化,例如,当我单击登录页面按钮时,URL 会从 localhost:3000 更改为 localhost:3000/register,但我没有在路由器中获取 console.log 并且页面不呈现。

谁能帮我解决这个错误?

【问题讨论】:

    标签: javascript backbone.js coffeescript marionette backbone-routing


    【解决方案1】:

    一个小时的调试,结果……一个愚蠢的拼写错误。

    如果您在Backbone.Router 中的routes 对象前面包含/路由将无法正常工作。我删除了正斜杠,一切正常。

    路线错误:

    routes:
        '/': 'landing'
        '/home': 'home'
        '/register': 'register'
    

    正确的路线:

    routes:
        '': 'landing'
        'home': 'home'
        'register': 'register'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2020-07-29
      • 2014-10-09
      • 2015-04-14
      • 2023-03-14
      相关资源
      最近更新 更多