Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created August 29, 2011 02:33
Show Gist options
  • Save mxriverlynn/1177645 to your computer and use it in GitHub Desktop.
Save mxriverlynn/1177645 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);
});
@panaggio
Copy link

Little typo fix here: https://gist.github.com/3755999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment