Sha256: 478ee51e925d59fea204bcc3098ad00b542ea195cc68702f019171c91a7bd5c2

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
revelry_core-0.1.12.6 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.12.5 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.12.4 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.12.3 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.12.2 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.12.0 app/assets/javascripts/revelry/router/routerlink.js.cjsx
revelry_core-0.1.11.6 app/assets/javascripts/revelry/router/routerlink.js.cjsx