所以我在我的登陆页面视图中有一个按钮,当点击它时,应该将我的用户路由到注册 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 并且页面没有呈现。
谁能帮我确定这个错误?