0

私は次のようなルーター設定をしています:

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れ、agentURLorderのセクションの末尾にあるIDから取得されます。uaoview

URLからこれらのパラメータを取得するためのデフォルトルートを取得するための最良の方法は何ですか?文字通りdocument.URLを分割しますか?

4

1 に答える 1

1

3 つのカスタム ルートをすべてデフォルトに配置するのではなく、1 つのカスタム ルートに統合すると読みやすくなります。

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

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

ただし、これは多くの異なる URL と一致することに注意してください。リソースごとに別々のルートを用意し、それらすべてを同じ機能にマップすることをお勧めします。

于 2012-08-01T19:52:42.533 に答える