Sha256: c6da6f6f279826b3b5c98e93a4a7872d1c33278784b65aabc875ac6d7ef3c2d8

Contents?: true

Size: 1.84 KB

Versions: 12

Compression:

Stored size: 1.84 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', class RouterLink extends React.Component {
  static get propTypes() {
    return {
      children: React.PropTypes.node,
      href: React.PropTypes.string,
      templatePath: React.PropTypes.string,
    }
  }

  render() {
    return <a onClick={this.onClick.bind(this)} {...this.props}>{this.props.children}</a>
  }

  navigationOptions() {
    return _(this.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(this.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(this.props.href, this.navigationOptions())
    }

    return 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() {
    return this.props.href[0] === '/' ? this.props.href.slice(1) : this.props.href
  }

  hasMatch() {
    return this.props.href !== null && typeof this.props.href !== 'undefined' && _(Backbone.history.handlers).any((handler) => {
      return handler.route.test(this.bbNormalHref())
    })
  }
})

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
revelry_core-0.1.24.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.23.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.22.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.21.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.20.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.19.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.18.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.17.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.16.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.15.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.14.0 app/assets/javascripts/revelry/router/routerlink.es6
revelry_core-0.1.13.0 app/assets/javascripts/revelry/router/routerlink.es6