Skip to content

Instantly share code, notes, and snippets.

@juliocesar
Created July 14, 2013 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliocesar/5993429 to your computer and use it in GitHub Desktop.
Save juliocesar/5993429 to your computer and use it in GitHub Desktop.
Controller-less router
# 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