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
Codeigniter Backbone.js-默认路由器,从URL获取数据_Codeigniter_Backbone.js - Fatal编程技术网

Codeigniter Backbone.js-默认路由器,从URL获取数据

Codeigniter Backbone.js-默认路由器,从URL获取数据,codeigniter,backbone.js,Codeigniter,Backbone.js,我的路由器设置如下: var AppRouter = Backbone.Router.extend({ routes: { "/agents/:id": "getAgent", "/orders/:id": "getOrders", "/users/:id": "getUsers", '*path': 'defaultRoute' }, getAgent: function(id) { t

我的路由器设置如下:

var AppRouter = Backbone.Router.extend({
    routes: {
        "/agents/:id": "getAgent",
        "/orders/:id": "getOrders",
        "/users/:id": "getUsers",
        '*path':  'defaultRoute'

    },
    getAgent: function(id) {
        ticketList.url = "/index.php/tickets/viewJSON/a"+id;
    },
    getOrders: function(id) {
        ticketList.url = "/index.php/tickets/viewJSON/o"+id;
    },
    getUsers: function(id) {
        ticketList.url = "/index.php/tickets/viewJSON/u"+id;
    },
    defaultRoute: function() {
        //(here needs to be  the code)   
    }

});
我使用的是codeigniter,所以当我访问
/index.php/agents/view/1103
时,我需要强制
ticketList.url
/index.php/tickets/viewJSON/a1103
-a取自
/agent/
(这将分别在
user
agent
order
u
a
o
之间更改,然后从URL的
视图
部分获取ID


获取从URL获取这些参数的默认路由的最佳方法是什么?按字面意思拆分document.URL?

如果将三个自定义路由合并为一个自定义路由,而不是将它们全部放在默认路由中,阅读起来会更容易

var AppRouter = Backbone.Router.extend({
    routes: {
        "/:resource/:id": "getResource"
    },

    getResource: function(resource, id) {
        ticketList.url = "/index.php/tickets/viewJSON/" + resource[0] + id;
    }
});

但请注意,这将匹配许多不同的URL,可能比您真正想要的要多。最好为每个资源设置单独的路由,并将它们映射到相同的功能上。

默认URL是什么?请参阅我对下面答案的评论,希望为输入清除一些内容,这肯定会使但是,在将来我会大量扩展每个函数。我想我可以在里面做if语句。然而,这实际上并没有回答我最初的问题:如果我访问
http://mysite.com/index.php/agents/view/1103
(显示codeigniter页面),我需要它来模拟
/index.php/agents/view/1103/#/agents/1103
,从而触发Backbone.js路由器。(这不是一个单页应用程序,而是一个多页应用程序,我使用Backbone创建更整洁的代码)