-_-#【Backbone】Router

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../app-min.css">
</head>
<body>
    <a href="#actions">testActions</a>
    <a href="#/posts/120">Post 120</a>
    <a href="#/download/user/images/hey.gif">download gif</a>
    <a href="#/dashboard/graph">Load Route/Action View</a>
    <script src="../../jquery/jquery-1.10.2.js"></script>
    <script src="../underscore.js"></script>
    <script src="../backbone.js"></script>
    <script>
        var AppRouter = Backbone.Router.extend({
            routes: {
                "posts/:id": "getPost",
                "download/*path": "downloadFile",
                ":route/:action": "loadView",
                "*actions": "defaultRoute"
            },
            getPost: function(id) {
                console.log(id)
            },
            defaultRoute: function(actions) {
                console.log(actions)
            },
            downloadFile: function(path) {
                console.log(path)
            },
            loadView: function(route, action) {
                console.log(route + '_' + action)
                app_router.navigate("/posts/" + 404, {trigger: true, replace: true})
            }
        })
        var app_router = new AppRouter
        Backbone.history.start()
    </script>
    <article class="content">
        <pre>var AppRouter = Backbone.Router.extend({
    routes: {
        "posts/:id": "getPost",
        "download/*path": "downloadFile",
        ":route/:action": "loadView",
        "*actions": "defaultRoute"
    },
    getPost: function(id) {
        console.log(id)
    },
    defaultRoute: function(actions) {
        console.log(actions)
    },
    downloadFile: function(path) {
        console.log(path)
    },
    loadView: function(route, action) {
        console.log(route + '_' + action)
        app_router.navigate("/posts/" + 404, {trigger: true, replace: true})
    }
})
var app_router = new AppRouter
Backbone.history.start()</pre>
    </article>
</body>
</html>
原文地址:https://www.cnblogs.com/jzm17173/p/4171838.html