Created
July 14, 2013 06:44
-
-
Save juliocesar/5993429 to your computer and use it in GitHub Desktop.
Controller-less router
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
# Application router | |
# ================== | |
# | |
# Written for https://medium.com/what-i-learned-building/89e504dd8313. | |
# This is how it'd look like without the pattern applied. | |
class Router extends Backbone.Router | |
routes: | |
'users/:id/posts' : -> | |
@requireLogin => | |
if App.User.drafts.isEmpty() | |
App.User.drafts.fetch | |
success: => | |
view = new DraftsView model: App.User | |
$('body').empty().append view.render().el | |
else | |
view = new DraftsView model: App.User | |
$('body').empty().append view.render().el | |
'drafts' : -> | |
if user = App.Users.get userId | |
view = new ViewPosts model: user | |
$('body').empty().append view.render().el | |
else | |
App.Users.fetch id: userId, | |
success: => | |
view = new ViewPosts model: user | |
$('body').empty().append view.render().el | |
error: => | |
# Show not found error | |
# A few helper method that will be used by | |
# our controllers above. | |
requireLogin: (success = ->) -> | |
if App.User.loggedIn() | |
success() | |
else | |
@go'/login-required' | |
go: (path) -> | |
@navigate path, trigger: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment