Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

obvious-vue

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obvious-vue

a library to help using obvious in vue2.x application

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

obvious-vue

Coverage Status release lastCommit

The official library to help you use obvious in Vue application

Install

npm:

npm install vue obvious-vue

umd:

<script src="https://unpkg.com/obvious-vue@{version}/dist/index.umd.js"></script>
import Vue from 'vue'
import ObviousVue from 'obvious-vue'

Vue.use(ObviousVue)

Usage

You should provide the $bus and $socket on the root, and they will be injected into all the child components and will be available on them as this.$bus and this.$socket

const $bus = window.__Bus__.host
const $socket = $bus.createSocekt()

const app = new Vue({
  $bus,
  $socket
})

app.$mount(document.getElementById('root'))

state

You can declare the obviousData option to perform the two-way binding for Vue data and obvious state

<template>
  <div :style="{color: theme}">{{ userName }}</div>
</template>

<script>
const anotherBus = window.__Bus__.anotherBus
const anotherSocket = anotherBus.createSocket()

export default {
  name: 'SubComponent',

  obviousData: {
    userName: 'user.name', // Two-way binding for this.userName and this.$socket.getState('user.name')
    theme: { // Two-way binding for this.theme and anotherSocket.getState('themeColor')
      state: 'themeColor',
      socket: anotherSocket
    }
  }
}
</script>

broadcast and unicast

You can declare the broadcast and unicast option to register some broadcast and unicast events for obvious

<template>
  <ul ref="container">
    <li v-for="item of list">{{ item }}</li>
  </div>
</template>

<script>
const anotherSocket = window.__Bus__.anotherBus.createSocket()

export default {
  data: {
    list: []
  },

  broadcast: {
    addItem(item) { // this.$socket.onBroadcast('addItem', handler)
      this.list.push(item)
    },
    addItemByAnotherSocket: { // anotherSocket.onBroadcast('addItemByAnotherSocket', handler)
      handler(item) {
        this.list.push(item)
      },
      socket: anotherSocket
    },
    deleteItem(index) {
      this.list.splice(index, 1)
    },
  },

  unicast: {
    getItem(index) {// this.$socket.onUnicast('getItem', handler)
      return this.list[index]
    },
    getItemByAnotherSocket:{// anotherSocket.onUnicast('getItem', handler)
      handler(index) {
        return this.list[index]
      },
      socket: anotherSocket
    }
  }
}
</script>

set the default socket

The default socket to handle all states and events is this.$socket, you can change it by declare the socket option

<script>
const anotherSocket = window.__Bus__.anotherBus.createSocket()
export default {
  socket,

  obviousData: {
    name: 'name' // Two-way binding for this.name and anotherSocket.getState('name')
  },
  broadcast: {
    changeName(value) { // anotherSocket.onBroadcast('changeName', handler)
      this.name = value
    },
    getName() { // anotherSocket.onUnicast('getName', handler)
      return this.name
    }
  }
}
</script>

activate obvious app

after using ObviousVue, there will be a global component named obvious-app to help you easily activate an obvious app, the app will be bootstrapped when the component is mounted and will be reactivated once the props activate-config is changed, and when the component is destroyed, the app will be destroyed too

const bus = window.__Bus__.host

bus.createApp('react-app')
  .bootstrap(async (config) => {
    ReactDOM.render(<App />, config.mountPoint)
  })
  .activate(async (config) => {
    console.log(config.message)
  })
  .destroy(async (config) => {
    console.log(config.message)
    ReactDOM.unmountComponentAtNode(config.mountPoint)
  })
<template>
  <obvious-app 
    ctx="react-app" 
    :activate-config="activateConfig"
    :destroy-config="destroyConfig"
  />
</template>

<script>
export default {
  data() {
    return {
      activateConfig: {
        message: 'I was activated by a vue app'
      },
      destroyConfig: {
        message: 'I was destroyed by a vue app'
      }
    }
  }
}
</script>

all the props and events of obvious-app are below

props:

namerequireddefaultdescription
busfalsethis.$busthe bus to activate the target app
ctxtrue-the context of the app to activate
activate-configfalse{}the config argument of activating
destroy-configfalse{}the config argument of destroying

events:

namedescription
activatedemitted after the app is activated
destroyedemitted after the app is destroyed
erroremmited when an error is throwed during activating or destroying

License

obvious-vue is MIT licensed

Keywords

FAQs

Package last updated on 01 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc