Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript backbone.js中的默认路径问题_Javascript_Backbone.js - Fatal编程技术网

Javascript backbone.js中的默认路径问题

Javascript backbone.js中的默认路径问题,javascript,backbone.js,Javascript,Backbone.js,我正在尝试创建我的第一个BB应用程序。一切正常,但我有个问题。我的路由器如下所示: var PlayersAppRouter = Backbone.Router.extend({ routes: { '': 'index', }, initialize: function () { this.model = new PlayersAppModel({}); this.view = new PlayersAppView({m

我正在尝试创建我的第一个BB应用程序。一切正常,但我有个问题。我的路由器如下所示:

var PlayersAppRouter = Backbone.Router.extend({
    routes: {
        '': 'index',
    },

    initialize: function () {
        this.model = new PlayersAppModel({});
        this.view = new PlayersAppView({model: this.model});
    },

    index: function () {
        alert('It works'); //<-- It doesn't
    },
});
在我的代码后面,我有:

$(function () {
        window.app = new PlayersAppRouter;
        Backbone.history.start({pushState: true});

        app.model.players.reset(<?php require('players.php'); ?>); //<-- players.php loads a bunch of JSON data.
});
现在,为什么路由器的索引操作没有启动?我做错什么了吗?这个代码还有其他问题吗


完整的应用程序可以在这里找到:

摆脱你的pushState:true at it works

pushSate可能有个bug。看这里


摆脱你的强迫状态:这是真的

pushSate可能有个bug。看这里


使用正则表达式,在initialize函数中手动添加路由

...
initialize: function() {
    this.route(/\/?/, 'index', this.index);
},
index: function() {
    alert('It works!');
},
...
MainView = new View.main();
appRouter = new Router();
// set up single page app with push state
Backbone.history.start({pushState: true});
appRouter.initialize();
这在{pushState:true}上运行良好


使用正则表达式,在initialize函数中手动添加路由

...
initialize: function() {
    this.route(/\/?/, 'index', this.index);
},
index: function() {
    alert('It works!');
},
...
MainView = new View.main();
appRouter = new Router();
// set up single page app with push state
Backbone.history.start({pushState: true});
appRouter.initialize();
这在{pushState:true}上运行良好


不记得我在哪里读到的,我发现你必须在路由器上调用初始化。这是我的工作示例,在jquery.ready函数中激发

...
initialize: function() {
    this.route(/\/?/, 'index', this.index);
},
index: function() {
    alert('It works!');
},
...
MainView = new View.main();
appRouter = new Router();
// set up single page app with push state
Backbone.history.start({pushState: true});
appRouter.initialize();
路由配置

routes: {
    "": "index"
}

使用@serg.io回答this.route/\/?/,“index”,this.index;导致它覆盖了我的其他路由。

记不起我在哪里读过它,我发现你必须在路由器上调用initialize。这是我的工作示例,在jquery.ready函数中激发

...
initialize: function() {
    this.route(/\/?/, 'index', this.index);
},
index: function() {
    alert('It works!');
},
...
MainView = new View.main();
appRouter = new Router();
// set up single page app with push state
Backbone.history.start({pushState: true});
appRouter.initialize();
路由配置

routes: {
    "": "index"
}

使用@serg.io回答this.route/\/?/,“index”,this.index;导致它覆盖了我的其他路由。

这覆盖了其他路由。查看其他路由上的fiddle-with-sub-url。查看fiddle-with子url