backbone>>router

来源:互联网 发布:淘宝网婴儿大pp裤 编辑:程序博客网 时间:2024/05/17 02:16
<html><head>    <title>backbone.js-Router</title></head><body>    <a href="#/posts/120">posts 120</a><br><br>    <a href="#/download/yyf/a.css">path</a><br><br>    <a href="#/dashboard/graph">load router/action view</a><br><br></body><script src="../../node_modules/jquery-2.0.2.min.js"></script><script src="../../node_modules/underscore-min.js"></script><script src="../../node_modules/backbone-min.js"></script><script>(function($){    //backbone的router是路由,是来控制url的    //backbone的router会将#标签当做是url路径    var Router = Backbone.Router.extend({        //注意这里是routes!!        //并且这里的routes是按照顺序来匹配的        //两种方式来传递参数:一个是':id',将id传过去,一个是'*action'可以匹配所有的参数        routes:{                    'posts/:id' : 'getPost',            'download/*path' : 'downloadFile',            ':router/:action' : 'loadView',            '*actions' : 'defaultsRouter'        },        getPost:function(id){            alert('id= '+id);        },        defaultsRouter:function(actions){            alert('actions= '+actions);            console.log('fail');        },        downloadFile:function(path){            alert('path= '+path);        },        loadView:function(router, action){            alert(router + '_'  + action);        }    });    var app_router = new Router;    //当所有的路由创建好的时候需要同过Backbone.history.start();来监听这些路由    Backbone.history.start();})(jQuery);</script></html>

0 0