Skip to content

Event bubbling #149

Closed
Closed
@punmechanic

Description

@punmechanic

Consider the following components:

Link.html

<button on:click='clicked()'>plz click</button>

<script>
  export default {
    methods: {
      clicked() {
        return this.fire('navigate')
      }
    }
  }
</script>

LandingPage.html

<h1>Hello, world!</h1>

<Link></Link>

<script>
  import Link from '../Link.html'

  export default {
    components: {
      Link
    }
  }
</script>

Router.html

<div ref:container></div>

<script>
  export default {
    onrender() {
      const RouteConstructor = this.get('route')
      const currentComponent = new RouteConstructor({})

      currentComponent.mount(this.refs.container)

      currentComponent.on('navigate', () => {
        console.log('test')
      })

      this.set({
        activeRouteHandler: currentComponent
      })
    },

    onteardown() {
      const activeRouteHandler = this.get('activeRouteHandler')

      if (activeRouteHandler !== undefined) {
        activeRouteHandler.teardown()
      }
    },

    computed: {
      route: routes => routes['/']
    }
  }
</script>

Main.html

<Router routes='{{ routes }}'/>

<script>
  import Router from './Router.html'
  import LandingPage from './scenes/LandingPage.html'

  const routes = {
    '/': LandingPage
  }

  export default {
    data() {
      return {
        routes
      }
    },

    components: {
      Router
    }
  }
</script>

Given the above, I was hoping that Link would propagate its navigate event up to the Router component, which would catch it and then act on that event. Am I doing something FUBAR here or is there an alternative pattern for orthogonal concerns like Routing? I would like to be able to just use a <Link> component without the rest of the application having to be concerned about "how" routing is done and have it "just work".

Currently, clicking the plz click button will not propagate the event up to Router, so nothing happens. The only other alternative I can see is by manipulating the history from the Link component, which sort-of works but I see that being problematic in terms of re-use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions