-
-
Save panaggio/3755999 to your computer and use it in GitHub Desktop.
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
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); | |
}); |
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
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