Sha256: 6e5b779076be7465f7737054fe7dcfdf073def300507b254806e712ccc91905c

Contents?: true

Size: 1.56 KB

Versions: 12

Compression:

Stored size: 1.56 KB

Contents

# Wraps a link with the logic necessary to trigger a navigate event on our
# router. If a route is not available on any router, fall back to normal link
# behavior.
Rev.registerComponent 'RouterLink',
  propTypes:
    href: React.PropTypes.string
    templatePath: React.PropTypes.string

  render: ->
    <a onClick={ @onClick } {...@props}>{ @props.children }</a>

  navigationOptions: ->
    _(@props).pick('templatePath')

  # Check for route match on any router and then trigger navigation if it is
  # supported. If it is not, we'll have normal link click behavior.
  onClick: (e)->
    # Any router matches
    if @hasMatch()
      e.preventDefault()
      # Save the current URL on the history stack, don't trigger callbacks
      # This means our back button can take us back to this page after we swap
      Backbone.history.navigate(Backbone.history.fragment, trigger: false)
      # Finally actually trigger the navigation & component swap
      App.router.navigateTo @props.href, @navigationOptions()
    true

  # Backbone routers don't behave well with a slash at the begining of a route
  # and also don't handle matching well when a URL starts with a slash. But we
  # want absolute paths in URLs so the fallback behavior will be right. Also, the
  # call to `Backbone.history.navigate` wants the slash? Trim it for matching
  # only.
  bbNormalHref: ->
    if @props.href[0] == '/' then @props.href[1..] else @props.href end

  hasMatch: ->
    if @props.href?
      _(Backbone.history.handlers).any (handler)=>
        handler.route.test(@bbNormalHref())
    else
      false

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
revelry_core-0.1.11.5 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.11.2 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.11.1 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.11.0 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.7 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.6 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.5 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.4 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.3 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.2 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.1 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.10.0 app/assets/javascripts/revelry/router/routerlink.js.cjsx