UsersRouter = Backbone.Router.extend
routes:
users: 'showUsers'
'users/:uuid': 'showUser'
initialize: (options) ->
_.bindAll @, 'switchToUsers', 'switchToUser'
@listenTo Backbone, 'showUsers', @switchToUsers
@listenTo Backbone, 'showUser', @switchToUser
UsersManager = Backbone.Manager.extend
states:
'users.detail':
url: 'users/:id'
loadMethod: 'showUser'
transitionMethod: 'switchToUser'
Backbone.Manager =
states: # the foundation
users.detail: # state name
url: 'users/:id' # (optional) used in History matching and updating
# will update browser url on state transition
loadMethod: 'showUser' # (optional) name of pageload callback
# url must be defined to activate
transitionMethod: 'switchUser' # name of state change callback
# Arguments are built from url params
showUser: (id) ->
# all of Arguments except Options are either provided by automatic
# trigger if url is defined, or passed through directly from go() call
#
# Options currently only contain the filled in url, so in this case:
# Options = {url: 'users/1'}
switchUser: (id, options) ->
Three Ways
events:
'click dd': 'showUser'
showUser: ->
Backbone.Manager.go('users.detail', {id:1})
<a data-bb-state='users.detail([1])'/>
<a data-bb-state='users.detail({id:1})'/>
<a data-bb-state='' href='/users/1'/>
Backbone.Manager.extend
states:
'*':
url: '*url'
transitionMethod: 'defaultTransition'
Backbone.Router limitations...
states:
a:
url: 'a/:id'
b:
url: '*'
a.url will never be matched by routerstates:
b:
url: '*'
a:
url: 'a/:id'
now a.url matches first, with b.url
as proper fallback