Backbone Test
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>
<div id='container'>
halo world
</div>
<button id='but1'>red view</button>
<button id='but2'>blue view</button>
CSS
#red_view{
width:400px;
height:400px;
background-color:red;
}
#blue_view{
width:400px;
height:400px;
background-color:blue;
}
.button,.button2{
width:300px;
height:300px;
background-color:gray;
}
JavaScript
RedView = Backbone.View.extend({
el: "#container",
events: {
"click .button": "clickme"
},
clickme: function() {
alert('redview');
},
initialize: function() {
_.bindAll(this, 'cleanup');
this.$el.html("<div id='red_view'><div class='button'>Click Me</div></div>");
},
cleanup: function() {
this.undelegateEvents();
$(this.el).empty();
}
});
BlueView = Backbone.View.extend({
el: "#container",
events: {
"click .button2": "clickme2"
},
clickme2: function() {
alert('blueview');
},
initialize: function() {
_.bindAll(this, 'cleanup');
this.$el.html("<div id='blue_view'><div class='button2'>Click Me</div></div>");
},
cleanup: function() {
this.undelegateEvents();
$(this.el).empty();
}
});
$(document).ready(function() {
//var router = new SystemRouter();
var view = null;
$('#but1').click(function() {
if (view) {
view.cleanup();
}
view = new RedView();
});
$('#but2').click(function() {
if (view) {
view.cleanup();
}
view = new BlueView();
});
});