npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-plugin-events

v1.0.2

Published

Simple global event bus for Vue.js applications

Downloads

35

Readme

Vue.js Events Plugin

npm version vue 2.x build status minzipped size code style: standard + prettier

Simple global event bus for Vue.js applications with automatic subscription control. Zero dependencies.



Installation

npm install --save vue-plugin-events

Setup

Bundler (Webpack, Rollup)

import Vue from 'vue'
import VueEvents from 'vue-plugin-events'

Vue.use(VueEvents)

Browser

<!-- Include after Vue -->
<!-- Local files -->
<script src="vue-plugin-events/dist/vue-plugin-events.umd.js"></script>

<!-- From CDN -->
<script src="https://cdn.jsdelivr.net/npm/vue-plugin-events"></script>

Usage

Listening to Events

The plugin will automatically subscribe a component to events defined within its body. This is achieved using a new section named events. When your component is destroyed, so are any listeners that have been automatically created.

The handlers follow a pattern similar to component watch section.

// MyComponent.vue
export default {
  data: () => ({
    myValue: 'anything'
  }),
  methods: {
    doSomething (arg) { /*...*/ }
  },
  events: {

    // will execute method `doSomething` when event named `someEvent` is emitted
    someEvent: 'doSomething',

    // will fire when event named `someHandledEvent` is emitted
    someHandledEvent (v) {
      this.doSomething(v)
    },

    // will fire when event named `immediateEvent` is emitted, and as
    // soon as the component is loaded and this section is evaluated,
    // much like `immediate` option from watchers
    immediateEvent: {
      immediate: true,
      handler (v) {
        this.doSomething(v)
      }
    },

    // will fire when a `justOnceEvent` is fired, and then won't fire again;
    // may fire twice if combined with `immediate`
    justOnceEvent: {
      once: true,
      handler (v) {
        console.log('arg is', v)
      }
    }

  }
}

Emitting Events

Every component will be inject with an instance of $event, which allows the developer to emit events into the application-wide event bus. Any components listening to the emitted event will be triggered. More details can be seen in the API section below.

The methods exposed by $event below are basically the same as normal event methods in a Vue component, but instead of having only the parent component as a listeners, any component can subscribe to any events.

// AnyComponent.vue
export default {
  // ...
  methods: {
    onSomething () {
      this.$events.emit('someEvent', this.payload)
    }
  },
  // ...
}

API ($events)

.emit

  • Arguments
    • {string} eventName
    • [...args]
  • Usage
    • Triggers an event on the event bus. Any additional arguments will be passed into the listener’s callback function.

.on

  • Arguments
    • {string | Array<string>} event (array only supported in Vue 2.2.0+)
    • {Function} callback
  • Usage
    • Listens for an event in the global event bus. Normally, it is easier to allow the plugin to control the subscription of events. However, special cases do exist, so you can manually listen for an event if need be; just keep in mind that the plugin does not track manual subscriptions, so you must remember to unsubscribe before the component is destroyed. The callback will receive all the additional arguments passed into the event-triggering method.

.once

  • Arguments
    • {string | Array<string>} event (array only supported in Vue 2.2.0+)
    • {Function} callback
  • Usage
    • Listens for an event in the global event bus, but only once. The listener will be removed once it triggers for the first time.

.off

  • Arguments
    • {string | Array<string>} event (array only supported in Vue 2.2.2+)
    • {Function} [callback]
  • Usage
    • Removes a global event bus listener. Both event and callback are necessary, to avoid removing more listeners than intended.

License

ISC © 2019 Ricardo Nolde