简体   繁体   English

Backbone.js路由器没有检测到网址更改

[英]Backbone.js Router not detecting url change

Looking at this example it appears that the router should detect links causing a change in URL that match its routes. 看一下这个例子 ,路由器似乎应检测到导致URL更改与其路由匹配的链接。 However, clicking on the link generated by my Handlebars template only changes the URL and does not trigger the alert in the router. 但是,单击我的Handlebars模板生成的链接只会更改URL,并且不会在路由器中触发警报。 The route is, for some strange reason, triggered upon refresh, though the URL looses the hash. 由于某些奇怪的原因,该路由在刷新时触发,尽管URL丢失了哈希。

I'm not sure why the router isn't picking up my URL change. 我不确定为什么路由器没有拿起我的URL更改。 I'd prefer using links rather than Router.navigate if they do work. 我更喜欢使用链接而不是Router.navigate,如果它们可以工作的话。 The route does work for the default route. 该路由适用于默认路由。

The backbone.js code below is run on $(document).ready from a script in the html head. 下面的backbone.js代码是在html头部的脚本中运行$(document).ready。

Item Handlebars template 项目把手模板

{{#with lobby}}
<li id="lobby-{{id}}">
    <div class="lobby_info">{{name}} - {{owner}} - {{player_count}}/18</div> <a href="#lobby/{{id}}">Join</a>
 </li>
 {{/with}}

Item Backbone View 项目骨干视图

var LobbyView = Backbone.View.extend({
    events: {
        "click .lobby_info": "expand_toggle",
    },

    expand_toggle: function(evt) {
        alert('Expand toggle');
    },

    render: function() {
        var template = Handlebars.compile($("#lobby_template").html());

        this.$el.html(template(this.model.toJSON()));

        return this;
    }
});

Item Collection View 项目集合视图

var LobbiesView = Backbone.View.extend({
    events: {
        "submit #create_lobby_form": "create_lobby",
    },

    create_lobby: function(evt) {
        evt.preventDefault()

        var val = $('#lobby_name').val();

        socket.emit('create', val);

        $("#lobby_name").val("");
    },

    initialize: function() {
        var me = this;

        socket.on('create', function(lobby) {
            var lobby_item = new LobbyView({
                model: new LobbyModel({
                    lobby: lobby
                })
            });

            // render it to the DOM
            el = lobby_item.render().el;
            me.$("#lobbies").append(el);
       });

    },

    render: function() {
        var me = this;

        var template = Handlebars.compile($("#lobbies_template").html());

        $(this.el).html(template());

        return this;
    },
});

Router 路由器

var Router = Backbone.Router.extend({
    // Match urls with methods
    routes: {
        "": "index",
        "lobby/:lobby_id": "lobby",
        "player/:player_id": "player"
    },

    index: function() {
        // connect to the websocket
        socket = io.connect();
        socket.emit('subscribe');
        var view = new LobbiesView({
            el: $("#container"),
        });

        view.render();
    },

    // View a lobby
    lobby: function(lobby_id) {
        alert('Viewing lobby' + lobby_id);
    },

    // View a player
    player: function(player_id) {
    },
});

// start backbone routing
var app = new Router();
Backbone.history.start({ pushState: true });

Your issue is { pushState: true } . 您的问题是{ pushState: true } Remove it and your router will work (tested in console). 删除它,你的路由器将工作(在控制台测试)。 If pushState is set to true, you need to set up routes without hashes. 如果pushState设置为true,则需要设置没有哈希的路由。

This is the code I was testing: 这是我测试的代码:

var Router = Backbone.Router.extend({
    routes: {
        "": "index",
        "lobby/:lobby_id": "lobby",
        "player/:player_id": "player",
        "*actions": "test"
    },

    test: function() { 
        console.log('test', arguments) 
    },

    index: function() {
        console.log('index', arguments);
    },

    player: function() {
        console.log('player', arguments);
    },

    lobby: function(lobby_id) {
        console.log('lobby', arguments);
    }
});

// start backbone routing
var app = new Router();
Backbone.history.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM