1

是否可以在不使用的情况下重写浏览器的历史记录history.pushState

我有一个带有路由器的骨干应用程序,看起来像这样:

routes: {
    'posts': 'posts',
    'modal': 'modal'
},

'before': function (args) {
    //check if logged in
        //do some various other setup tasks
    //maybe delete modal routes from the history?

    //forward to route
    args.next()
},

'posts': function () {
    //make and show posts
},

'modal': function (params) {
    //edit user email preferences OR
    //create a new post OR
    //do various other crud operations
}

每当触发模式路由时,我都想防止将哈希写入浏览器的历史状态。原因是,用户在回溯历史时不应该重新打开模式。

我查看了主干源代码,但没有看到任何简单的内容history = []

有没有办法在没有推送状态的情况下做到这一点?

4

1 回答 1

0

您是否尝试将 router.navigate(fragment, [options]) 函数的 replace 选项设置为 true ?

这应该更新 URL 而不会在浏览器的历史记录中创建条目

http://backbonejs.org/#Router-navigate

如果您正在处理按钮单击事件,则应该在视图中而不是在路由器类中处理。

例子:

class MyView extends Backbone.View

    events :

        'click #backButton'    :    'onClick'


    initialize : ->

           #Some Stuff


    render: ->

           #Some Stuff


    onClick: ( e ) ->

           router.navigate '/projects', {replace : true }
于 2013-02-25T23:25:15.157 回答