Skip to content

Instantly share code, notes, and snippets.

@jacobthemyth
Last active August 29, 2015 14:15
Show Gist options
  • Save jacobthemyth/157f91f545dfc047d1d9 to your computer and use it in GitHub Desktop.
Save jacobthemyth/157f91f545dfc047d1d9 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app"></div>
<script type="text/template" id="toot-input-template">
<input type="text" placeholder="Toot">
</script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/backbone/backbone.js"></script>
<script src="main.js"></script>
</body>
</html>
(function(){
var Toot = Backbone.Model.extend({
defaults: {
body: ""
}
});
var TootsCollection = Backbone.Collection.extend({
model: Toot
});
var TootInputView = Backbone.View.extend({
tagName: 'form',
events: {
'submit': 'createToot'
},
createToot: function(e){
e.preventDefault();
console.log('toot');
},
template: _.template( $('#toot-input-template').text() ),
render: function(){
this.$el.html( this.template() );
return this;
}
});
var TootsListView = Backbone.View.extend({
tagName: 'ul',
render: function(){
var self = this;
this.collection.each(function(toot){
self.$el.append('<li>' + toot.get('body') + '</li>');
});
}
});
var TooterRouter = Backbone.Router.extend({
routes: {
'': 'index',
'toot/:id': 'showToot'
},
initialize: function(){
this.toots = new TootsCollection([{body: 'First Toot'}]);
this.inputView = new TootInputView({collection: this.toots});
this.listView = new TootsListView({collection: this.toots});
},
index: function(){
this.inputView.render();
$('#app').append(this.inputView.el);
this.listView.render();
$('#app').append(this.listView.el);
},
showToot: function(id){
console.log(id);
}
});
$(document).ready(function(){
var router = new TooterRouter();
Backbone.history.start();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment