Appearance
Bus Plugin
File Information
- Path:
plugins/bus.js - Purpose: Vue instance-based event bus for component communication
Overview
The Bus plugin creates a global Vue instance that can be used as an event bus for communication between components. It's available as $bus on all Vue instances and provides a simple pub/sub pattern for component communication.
Key Features
Event Bus
- Global Vue instance
- Event emission
- Event listening
- Component communication
Simple API
$emit- Emit events$on- Listen to events$off- Remove listeners$once- Listen once
Installation
The plugin automatically installs itself:
- Creates Vue instance
- Attaches to
Vue.prototype.$bus - Available globally
Usage Examples
Emit Event
javascript
// In component
this.$bus.$emit('asset-uploaded', assetData)
this.$bus.$emit('folder-created', folderData)Listen to Event
javascript
// In component
export default {
mounted() {
this.$bus.$on('asset-uploaded', this.handleAssetUploaded)
},
beforeDestroy() {
this.$bus.$off('asset-uploaded', this.handleAssetUploaded)
},
methods: {
handleAssetUploaded(assetData) {
// Handle event
console.log('Asset uploaded:', assetData)
}
}
}Listen Once
javascript
// In component
this.$bus.$once('workspace-switched', (workspace) => {
// Handle only first occurrence
this.loadWorkspaceData(workspace)
})Remove Listener
javascript
// In component
this.$bus.$off('event-name', handler)
// Or remove all listeners for event
this.$bus.$off('event-name')Common Events
Common events used in the application:
asset-uploaded- Asset upload completedfolder-created- Folder createdworkspace-switched- Workspace changedfilter-applied- Filter appliedsearch-executed- Search performed
Best Practices
- Cleanup: Always remove listeners in
beforeDestroy - Naming: Use descriptive event names
- Data: Pass minimal data in events
- Documentation: Document custom events
Integration Points
- Components: Inter-component communication
- Pages: Cross-page communication
- Store: Event-driven updates
Notes for Development
- Plugin creates global event bus
- Available as
$buson all components - Remember to cleanup listeners
- Use for loose coupling between components
- Alternative to props/events for distant components
Related Documentation
- Event Bus Plugin - Alternative event bus
- Vue.js Events - Vue events