Skip to content

Instantly share code, notes, and snippets.

@panaggio
Forked from mxriverlynn/1-firstimpl.js
Created September 20, 2012 13:40
Show Gist options
  • Save panaggio/3755999 to your computer and use it in GitHub Desktop.
Save panaggio/3755999 to your computer and use it in GitHub Desktop.
BlogPost = Backbone.Model.extend({
select: function(){
this.set({selected: true});
this.collection.selectPost(this);
}
});
BlogPosts = Backbone.Collection.extend({
model: BlogPost,
selectPost: function(post){
this.vent.trigger("post:selected", post);
}
});
BlogPostPreview = Backbone.View.extend({
events: {
"click a": "postSelected"
},
initialize: function(){
this.model.bind("change:selected", this.highlight, this);
},
highlight: function(model, selected){
var cssClass = ""
if (selected){
cssClass = "highlight"
}
$(this.el).attr("class", cssClass);
},
postSelected: function(e){
e.preventDefault();
this.model.select();
}
})
BlogRouter = Backbone.Router.extend({
routes: {
"/post/:id": "showPost"
},
showPost: function(id){
var post = this.posts.get(id);
// load the post view and display it
}
});
var vent = _.extend({}, Backbone.Events);
var posts = new BlogPosts({vent: vent});
var router = new BlogRouter();
vent.bind("post:selected", function(post){
router.navigate("/post/" + post.id);
});
BlogPost = Backbone.Model.extend({
select: function(){
this.set({selected: true});
this.collection.selectPost(this);
}
});
BlogPosts = Backbone.Collection.extend({
model: BlogPost,
selectPost: function(post){
this.vent.trigger("post:selected", post);
}
});
BlogPostPreview = Backbone.View.extend({
events: {
"click a": "postSelected"
},
initialize: function(){
this.model.bind("change:selected", this.highlight, this);
},
highlight: function(model, selected){
var cssClass = ""
if (selected){
cssClass = "highlight"
}
$(this.el).attr("class", cssClass);
},
postSelected: function(e){
e.preventDefault();
this.model.select();
}
})
BlogRouter = Backbone.Router.extend({
routes: {
"/post/:id": "showPost"
},
initialize: function(options){
this.controller = options.controller;
},
showPost: function(id){
var post = this.posts.get(id);
post.select();
}
});
BlogController = function(){
this.showPost(post){
// load up the post view
// and display it
}
}
var vent = _.extend({}, Backbone.Events);
var posts = new BlogPosts({vent: vent});
var controller = new BlogController();
var router = new BlogRouter({controller: controller});
vent.bind("post:selected", function(post){
controller.showPost(post);
router.navigate("/post/" + post.id);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment