Backbone - Hashtag出现在URL

时间:2016-09-26 21:35:03

标签: javascript backbone.js

我正在做我的第一个Backbone项目。我使用的是Backbone 1.3.3和Underscore 1.8.3。我的路由器出了问题。当我调用路由器时,它将hashtag放在URL的最末端:

http://server:9999/backbone_demo/addMember#

而不是:

http://server:9999/backbone_demo/#addMember

这就是我想要的。

var router = null;
var context = "backbone_demo";

DemoRouter = Backbone.Router.extend({

    initialize: function () {
        initializeEventsForNavigation();
    },
    routes: {"" : "start", "/bands": "bands", "/addMember": "addMember", "*defaultRoute" : "defaultRoute"},

    defaultRoute: function() {
        this.start();
    },
    start: function () {
        this.bands();
    },
    bands: function () {
        BackboneDemo.View.unrenderAllViews();
        BackboneDemo.View.renderBandList();
    },
    addMember: function() {
        BackboneDemo.View.unrenderAllViews();
        BackboneDemo.View.renderAddMember();
    }


});

var init = function() {
    initializeEventsForBandListRender();
    initializeEventsForAddMemberRender();
    router = new DemoRouter();
    Backbone.history.start({pushState: true, root: context});

};

var navigate = function(e) {
    var route = e.detail.route;
    router.navigate("/" + route, true);
};

如果我将“addMember”传递给我的导航功能(在e.detail.route中),我将把“/ addMember”传递给router.navigate。除了这个细节,路由器工作正常。

1 个答案:

答案 0 :(得分:1)

如果您想使用哈希路由,我认为您应该将pushState设置为false

Backbone.history.start({pushState: false, root: context});