Skip to content

Commit

Permalink
fix: this problem
Browse files Browse the repository at this point in the history
fix #5
  • Loading branch information
yangmingshan committed Mar 23, 2019
1 parent 8c920e9 commit 65547df
Show file tree
Hide file tree
Showing 12 changed files with 2,853 additions and 1,939 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
dist/
__tests__/
6 changes: 0 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module.exports = {
root: true,
parserOptions: {
sourceType: 'module'
},
env: {
browser: true
},
extends: 'standard',
rules: {
'space-before-function-paren': [2, 'never']
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ You can install it via [yarn](https://yarnpkg.com) or [npm](https://npmjs.com).
$ yarn add vue-bus
$ npm install vue-bus --save
```
And it's available on [jsdelivr](https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.js) or [unpkg](https://unpkg.com/vue-bus/dist/vue-bus.js).
```html
<!-- development version -->
<script src="https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.js"></script>

<!-- production version -->
<script src="https://cdn.jsdelivr.net/npm/vue-bus/dist/vue-bus.min.js"></script>
```
When used with a module system, you must explicitly install the bus via Vue.use():
```js
import Vue from 'vue';
Expand Down
55 changes: 52 additions & 3 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global test expect */
const Vue = require('vue')
const VueBus = require('../dist/vue-bus.common')

Expand All @@ -9,12 +10,12 @@ test('Vue.bus', () => {
return { count: 0 }
},
created() {
this.$bus.on('add', num => { this.count += num })
this.$bus.once('addOnce', num => { this.count += num })
Vue.bus.on('add', num => { this.count += num })
Vue.bus.once('addOnce', num => { this.count += num })
},
methods: {
clean() {
this.$bus.off('add')
Vue.bus.off('add')
}
}
})
Expand Down Expand Up @@ -88,3 +89,51 @@ test('this.$bus', () => {
vm2.fireOnce()
expect(vm1.count).toBe(3)
})

test('pass bus function as argument', () => {
const vm = new Vue({
data() {
return { count: 0 }
},
created() {
this.listen(this.$bus.on)
this.listenOnce(this.$bus.once)
},
methods: {
listen(on) {
on('add', num => { this.count += num })
},
listenOnce(once) {
once('addOnce', num => { this.count += num })
},
clean(off) {
off('add')
}
}
})

const obj = {
fire(emit) {
emit('add', 1)
},
fireOnce(emit) {
emit('addOnce', 1)
}
}

obj.fire(Vue.bus.emit)
expect(vm.count).toBe(1)

obj.fire(Vue.bus.emit)
expect(vm.count).toBe(2)

vm.clean(Vue.bus.off)
obj.fire(Vue.bus.emit)
expect(vm.count).toBe(2)

obj.fireOnce(Vue.bus.emit)
expect(vm.count).toBe(3)

obj.fireOnce(Vue.bus.emit)
expect(vm.count).toBe(3)
})
18 changes: 11 additions & 7 deletions dist/vue-bus.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vue-bus v1.1.0
/*!
* vue-bus v1.2.1
* https://github.com/yangmingshan/vue-bus
* @license MIT
*/
Expand All @@ -11,27 +11,31 @@ function VueBus(Vue) {
Object.defineProperties(bus, {
on: {
get: function get() {
return this.$on
return this.$on.bind(this)
}
},
once: {
get: function get() {
return this.$once
return this.$once.bind(this)
}
},
off: {
get: function get() {
return this.$off
return this.$off.bind(this)
}
},
emit: {
get: function get() {
return this.$emit
return this.$emit.bind(this)
}
}
});

Vue.bus = bus;
Object.defineProperty(Vue, 'bus', {
get: function get() {
return bus
}
});

Object.defineProperty(Vue.prototype, '$bus', {
get: function get() {
Expand Down
18 changes: 11 additions & 7 deletions dist/vue-bus.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vue-bus v1.1.0
/*!
* vue-bus v1.2.1
* https://github.com/yangmingshan/vue-bus
* @license MIT
*/
Expand All @@ -9,27 +9,31 @@ function VueBus(Vue) {
Object.defineProperties(bus, {
on: {
get: function get() {
return this.$on
return this.$on.bind(this)
}
},
once: {
get: function get() {
return this.$once
return this.$once.bind(this)
}
},
off: {
get: function get() {
return this.$off
return this.$off.bind(this)
}
},
emit: {
get: function get() {
return this.$emit
return this.$emit.bind(this)
}
}
});

Vue.bus = bus;
Object.defineProperty(Vue, 'bus', {
get: function get() {
return bus
}
});

Object.defineProperty(Vue.prototype, '$bus', {
get: function get() {
Expand Down
82 changes: 43 additions & 39 deletions dist/vue-bus.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
/**
* vue-bus v1.1.0
/*!
* vue-bus v1.2.1
* https://github.com/yangmingshan/vue-bus
* @license MIT
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.VueBus = factory());
}(this, (function () { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.VueBus = factory());
}(this, function () { 'use strict';

function VueBus(Vue) {
var bus = new Vue();
function VueBus(Vue) {
var bus = new Vue();

Object.defineProperties(bus, {
on: {
get: function get() {
return this.$on
}
},
once: {
get: function get() {
return this.$once
Object.defineProperties(bus, {
on: {
get: function get() {
return this.$on.bind(this)
}
},
once: {
get: function get() {
return this.$once.bind(this)
}
},
off: {
get: function get() {
return this.$off.bind(this)
}
},
emit: {
get: function get() {
return this.$emit.bind(this)
}
}
},
off: {
});

Object.defineProperty(Vue, 'bus', {
get: function get() {
return this.$off
return bus
}
},
emit: {
});

Object.defineProperty(Vue.prototype, '$bus', {
get: function get() {
return this.$emit
return bus
}
}
});

Vue.bus = bus;

Object.defineProperty(Vue.prototype, '$bus', {
get: function get() {
return bus
}
});
}
});
}

if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(VueBus);
}
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(VueBus);
}

return VueBus;
return VueBus;

})));
}));
6 changes: 3 additions & 3 deletions dist/vue-bus.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 25 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
"name": "vue-bus",
"version": "1.2.0",
"version": "1.2.1",
"description": "A event bus for Vue.js",
"main": "dist/vue-bus.common.js",
"module": "dist/vue-bus.esm.js",
"unpkg": "dist/vue-bus.js",
"jsdelivr": "dist/vue-bus.js",
"typings": "types/index.d.ts",
"files": [
"src",
"dist/*.js",
"types/*.d.ts"
],
"scripts": {
"lint": "eslint src",
"lint": "eslint src __tests__",
"build": "rimraf dist && rollup -c && uglifyjs dist/vue-bus.js -c -m --comments -o dist/vue-bus.min.js",
"test": "jest && codecov"
},
Expand All @@ -25,24 +33,23 @@
},
"homepage": "https://github.com/yangmingshan/vue-bus#readme",
"devDependencies": {
"buble": "^0.18.0",
"codecov": "^3.0.0",
"eslint": "^4.16.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"jest": "^22.1.4",
"rimraf": "^2.6.2",
"rollup": "^0.55.0",
"rollup-plugin-buble": "^0.18.0",
"uglify-js": "^3.3.8",
"vue": "^2.5.13"
"buble": "^0.19.7",
"codecov": "^3.2.0",
"eslint": "^5.15.3",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.5.0",
"rimraf": "^2.6.3",
"rollup": "^1.7.0",
"rollup-plugin-buble": "^0.19.6",
"uglify-js": "^3.5.1",
"vue": "^2.6.10"
},
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
},
"typings": "types/index.d.ts"
}
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { version } from './package.json'
import buble from 'rollup-plugin-buble'

const banner = `/**
const banner = `/*!
* vue-bus v${version}
* https://github.com/yangmingshan/vue-bus
* @license MIT
Expand Down
Loading

0 comments on commit 65547df

Please sign in to comment.