Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Backbone.jsのroute機能をlocalで実行できるsample
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="https://raw.github.com/LearnBoost/socket.io-client/master/dist/socket.io.min.js"></script>
<script type="text/javascript" src="http://backbonejs.org/backbone-min.js"></script>
<script type="text/javascript">
var ExampleRouter = Backbone.Router.extend({
routes: {
"greeting/:language": "greeting",
".*": "selecting"
},
greeting: function(language) {
var msg = ""
switch(language){
case "spanish":
msg = "!hola!";
break;
case "english":
msg = "Hello!";
break;
default:
msg = "ごめんやしておくれやしてごめんやっしゃー";
break;
}
$('#contents').html(msg + '<br/><a href="#">back</a>');
},
selecting: function(){
$('#contents').html(
'<a href="#/greeting/spanish">Spanish</a><br/>' +
'<a href="#/greeting/english">English</a><br/>' +
'<a href="#/greeting/japanese">Japanese</a><br/>'
);
}
});
jQuery(function() {
window.router = new ExampleRouter();
Backbone.history.start();
});
</script>
</head>
<body>
<div id="contents"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment