-
-
Save majornorth/9220e129f2bc0a05ff73 to your computer and use it in GitHub Desktop.
Mosh Hamedani's approach to creating lists+todos with BackboneRelational
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script> | |
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script> | |
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div id="container"> | |
<div id="masters" class="region"></div> | |
<div id="detail" class="region"></div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.region { | |
width: 50%; | |
float: left; | |
border: 1px solid #ccc; | |
box-sizing: border-box; | |
padding: 30px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// App | |
var App = App || {}; | |
App.eventBus = _.extend({}, Backbone.Events); | |
// Models | |
App.Master = Backbone.Model.extend(); | |
App.Masters = Backbone.Collection.extend({ | |
Model: App.Master | |
}); | |
// Views | |
App.MasterView = Backbone.View.extend({ | |
events: { | |
"click": "onClick" | |
}, | |
onClick: function(){ | |
App.eventBus.trigger("master:select", this.model); | |
}, | |
render: function(){ | |
this.$el.html("<a href='#'>" + this.model.get("name") + "</a>"); | |
return this; | |
} | |
}); | |
App.MastersView = Backbone.View.extend({ | |
render: function(){ | |
var self = this; | |
_.each(this.collection, function(p){ | |
var masterView = new App.MasterView({ model: p }); | |
self.$el.append(masterView.render().$el); | |
}); | |
return this; | |
} | |
}); | |
App.DetailView = Backbone.View.extend({ | |
initialize: function(){ | |
App.eventBus.on("master:select", this.onMasterSelected, this); | |
}, | |
onMasterSelected: function(master){ | |
this.model = master; | |
this.render(); | |
}, | |
render: function(){ | |
if (!this.model) { | |
this.$el.html("Please select an item from the master list."); | |
} else { | |
this.$el.html(this.model.get("name")); | |
} | |
return this; | |
} | |
}); | |
$(document).ready(function(){ | |
var masters = [ | |
new App.Master({ name: "Master 1" }), | |
new App.Master({ name: "Master 2" }) | |
]; | |
var mastersView = new App.MastersView({ collection: masters }); | |
$("#masters").html(mastersView.render().$el); | |
var detailView = new App.DetailView(); | |
$("#detail").html(detailView.render().$el); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment