關於這個router的使用,我現在依然是心存疑惑的。每點擊一次這樣的連結action會觸發一個事件,但是url也會改變,這樣刷性的話,豈不是會自動觸發事件。或者這個東西只是用在單個頁面的網站上,或者行動裝置網站上,或者是我還不會用。
大概解釋下Router:
Backbone中的router,見名知意,router有路由的意思,顯然這裡是要控制url的。
Backbone.Router會把你串連中的#標籤當做是url路徑
即便我心存疑惑,依然是要寫幾個例子測試一下的。畢竟實踐才能解惑。
1、一個簡單的例子
var AppRouter = Backbone.Router.extend({
routes: {
"*actions" : "defaultRoute"
},
defaultRoute : function(actions){
alert(actions);
}
});
var app_router = new AppRouter;
Backbone.history.start();
需要通過調用Backbone.history.start()方法來初始化這個Router
這個Router的使用很像是django的urlconf檔案提供的功能,如果你懂得django的話。
在頁面上需要有這樣的a標籤:testActions
2、這個routes映射要怎麼傳參數呢
看下面例子,立馬你就知道了
var AppRouter = Backbone.Router.extend({
routes: {
"/posts/:id" : "getPost",
"*actions" : "defaultRoute"
},
getPost: function(id) {
alert(id);
},
defaultRoute : function(actions){
alert(actions);
}
});
var app_router = new AppRouter;
Backbone.history.start();
對應的頁面上應該有一個超連結:Post 120
從上面已經可以看到匹配#標籤之後內容的方法,有兩種:一種是用“:”來把#後面的對應的位置作為參數;還有一種是“*”,它可以匹配所有的url,下面再來演練一下。
[javascript] var AppRouter = Backbone.Router.extend({
routes: {
"/posts/:id" : "getPost",
"/download/*path": "downloadFile", //對應的連結為<a href="#/download/user/images/hey.gif">download gif</a>
"/:route/:action": "loadView", //對應的連結為<a href="#/dashboard/graph">Load Route/Action View</a>
"*actions" : "defaultRoute"
},
getPost: function(id) {
alert(id);
},
defaultRoute : function(actions){
alert(actions);
},
downloadFile: function( path ){
alert(path); // user/images/hey.gif
},
loadView: function( route, action ){
alert(route + "_" + action); // dashboard_graph
}
});
var app_router = new AppRouter;
Backbone.history.start();
var AppRouter = Backbone.Router.extend({
routes: {
"/posts/:id" : "getPost",
"/download/*path": "downloadFile", //對應的連結為<a href="#/download/user/images/hey.gif">download gif</a>
"/:route/:action": "loadView", //對應的連結為<a href="#/dashboard/graph">Load Route/Action View</a>
"*actions" : "defaultRoute"
},
getPost: function(id) {
alert(id);
},
defaultRoute : function(actions){
alert(actions);
},
downloadFile: function( path ){
alert(path); // user/images/hey.gif
},
loadView: function( route, action ){
alert(route + "_" + action); // dashboard_graph
}
});
var app_router = new AppRouter;
Backbone.history.start();
總結,router的使用看起來能夠去除通過對dom節點的綁定來觸發事件的那種繁瑣,但唯一讓我覺得不爽的就是點擊之後如果再重新整理,就會重新執行一遍對應的方法,因為url已經變了。或許這個也是可以解決的問題,只是我還沒有發現。
另外,在其他的模組中(指:model,view,collection),也可以通過使用routes:{}來根據連結觸發函數。
下面給出完整的代碼,注釋自己去掉實驗:
[javascript] <!DOCTYPE html>
<html>
<head>
<title>the5fire-backbone-router</title>
</head>
<body>
<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>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script>
(function ($) {
//Backbone中的router,見名知意,router有路由的意思,顯然這裡是要控制url的。
//Backbone.Router會把你串連中的#標籤當做是url路徑
/**
//1、來看一個簡單的例子
var AppRouter = Backbone.Router.extend({
routes: {
"*actions" : "defaultRoute"
},
defaultRoute : function(actions){
alert(actions);
}
});
var app_router = new AppRouter;
Backbone.history.start();
//2、既然是對url進行匹配那麼它應該不僅僅只是簡單的靜態匹配,應該具有傳遞參數的功能,所以下面再來一個動態router的例子.
var AppRouter = Backbone.Router.extend({
routes: {
"/posts/:id" : "getPost",
"*actions" : "defaultRoute"
},
getPost: function(id) {
alert(id);
},
defaultRoute : function(actions){
alert(actions);
}
});
var app_router = new AppRouter;
Backbone.history.start();
**/
//從上面已經可以看到匹配#標籤之後內容的方法,有兩種:一種是用“:”來把#後面的對應的位置作為參數;還有一種是“*”,它可以匹配所有的url,下面再來演練一下。
var AppRouter = Backbone.Router.extend({
routes: {
"/posts/:id" : "getPost",
"/download/*path": "downloadFile", //對應的連結為<a href="#/download/user/images/hey.gif">download gif</a>
"/:route/:action": "loadView", //對應的連結為<a href="#/dashboard/graph">Load Route/Action View</a>
"*actions" : "defaultRoute"
},
getPost: function(id) {
alert(id);
},
defaultRoute : function(actions){
alert(actions);
},
downloadFile: function( path ){
alert(path); // user/images/hey.gif
},
loadView: function( route, action ){
alert(route + "_" + action); // dashboard_graph
}
});
var app_router = new AppRouter;
Backbone.history.start();
})(jQuery);
</script>
</html>
摘自 the_fire的技術部落格