Skip to content

Commit f2b7b44

Browse files
alexsasharegantmorehouse
authored andcommitted
fix(root-listeners): apply listen-on-root mixin to other components (bootstrap-vue#684)
* fix(root-listeners): apply listen-on-root mixin to other components
1 parent 2ee9ed6 commit f2b7b44

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

docs/components/table/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ custom rendering, events, and asynchronous data.
4646
<pre>{{ modalDetails.data }}</pre>
4747
</b-modal>
4848

49-
</div>
49+
</div>
5050
</template>
5151

5252
<script>
@@ -130,7 +130,7 @@ export default {
130130
```
131131

132132
### `fields` prop
133-
The `fields` prop is used to display table columns.
133+
The `fields` prop is used to display table columns.
134134
Keys are used to extract real value from each row.
135135
Example format:
136136
```js
@@ -175,7 +175,7 @@ in the Vue.js guide.
175175
name: 'Havij'
176176
},
177177
{
178-
_cellVariants: {
178+
_cellVariants: {
179179
age: 'danger', // Displayes cell for field 'age' red
180180
name: 'success' // Displayes cell for field 'name' green
181181
},
@@ -198,7 +198,7 @@ the record, will be prefered.
198198

199199
`items` can also be a reference to a *provider* function, which returns an `Array` of items data.
200200
Provider functions can also be asynchronous:
201-
- By returning `null` (or `undefined`) and calling a callback, when the data is
201+
- By returning `null` (or `undefined`) and calling a callback, when the data is
202202
ready, with the data array as the only argument to the callback,
203203
- By returning a `Promise` that resolves to an array.
204204

@@ -297,7 +297,7 @@ The slot's scope variable (`data` in the above sample) will have the following p
297297
| `item` | Object | The entire record (i.e. `items[index]`) for this row
298298
| `index` | Number | The row number (zero based)
299299

300-
**Note** that `index` will not always be the actual row's index number, as it is
300+
**Note** that `index` will not always be the actual row's index number, as it is
301301
computed after pagination and filtering have been applied to the original
302302
table data. The `index` value will refer to the **displayed row number**. This
303303
number will align with the indexes from the optional `v-model` bound variable.
@@ -344,8 +344,8 @@ If you bind a variable to the `v-model` prop, the contents of this variable will
344344
be the currently disaplyed item records (zero based index, up to `page-size` - 1).
345345
This variable (the `value` prop) should usually be treated as readonly.
346346

347-
The records within the v-model are a filtered/paginated shallow copy of `items`, and
348-
hence any changes to a record's properties in the v-model will be reflected in
347+
The records within the v-model are a filtered/paginated shallow copy of `items`, and
348+
hence any changes to a record's properties in the v-model will be reflected in
349349
the original `items` array (except when `items` is set to a provider function).
350350
Deleteing a record from the v-model will **not** remove the record from the
351351
original items array.
@@ -372,7 +372,7 @@ The built-in default `sort-compare` function sorts the specified field `key` bas
372372
on the data in the underlying record object. The field value is first stringified
373373
if it is an object, and then sorted.
374374

375-
The default `sort-compare` routine **cannot** sort neither virtual columns, nor
375+
The default `sort-compare` routine **cannot** sort neither virtual columns, nor
376376
based on the custom rendering of the field data (which is used only for presentation).
377377
For this reason, you can provide your own custom sort compare routine by passing a
378378
function reference to the prop `sort-compare`.
@@ -402,7 +402,7 @@ if (typeof a[key] === 'number' && typeof b[key] === 'number') {
402402
```
403403

404404
### Using Items Provider Functions
405-
As mentioned under the `items` prop section, it is possible to use a function to provide
405+
As mentioned under the `items` prop section, it is possible to use a function to provide
406406
the row data (items), by specifying a function reference via the `items` prop.
407407

408408
**Note:** The `items-provider` prop has been deprecated in favour of providing a function
@@ -433,7 +433,7 @@ function myProvider(ctx) {
433433
let items = [];
434434

435435
// perform any items processing needed
436-
436+
437437
// Must return an array
438438
return items || [];
439439
}
@@ -471,7 +471,7 @@ function myProvider(ctx) {
471471
}
472472
```
473473

474-
`<b-table>` provides a `busy` prop that will flag the table as busy, which you can
474+
`<b-table>` provides a `busy` prop that will flag the table as busy, which you can
475475
set to `true` just before your async fetch, and then set it to `false` once you have
476476
your data, and just before you send it to the table for display. Example:
477477

@@ -499,7 +499,7 @@ methods: {
499499
}
500500
```
501501

502-
By default, the items provider function is responsible for **all paging, filtering, and sorting**
502+
By default, the items provider function is responsible for **all paging, filtering, and sorting**
503503
of the data, before passing it to `b-table` for display.
504504

505505
You can disable provider paging, filtering, and sorting (individually) by setting the
@@ -520,7 +520,7 @@ trigger the calling of the provider function. So be sure to bind to the `per-pa
520520
(unless you have the respective `no-provider-*` prop set to `true`).
521521

522522
#### Event based refreshing of data:
523-
You may also trigger the refresh of the provider function by emitting the
523+
You may also trigger the refresh of the provider function by emitting the
524524
event `table::refresh` on `$root` with the single argument being the `id` of your `b-table`.
525525
You must have a unique ID on your table for this to work.
526526

lib/components/table.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575

7676
<script>
7777
import { warn } from '../utils';
78-
import { keys } from '../utils/object.js'
78+
import { keys } from '../utils/object.js';
79+
import { listenOnRootMixin } from '../mixins'
7980
8081
const toString = v => {
8182
if (!v) {
@@ -117,6 +118,7 @@
117118
};
118119
119120
export default {
121+
mixins: [listenOnRootMixin],
120122
data() {
121123
return {
122124
sortBy: null,
@@ -278,7 +280,7 @@
278280
if (this.hasProvider) {
279281
this._providerUpdate();
280282
}
281-
this.$root.$on('table::refresh', (id) => {
283+
this.listenOnRoot('table::refresh', (id) => {
282284
if (id === this.id) {
283285
this._providerUpdate();
284286
}
@@ -478,7 +480,7 @@
478480
_providerSetLocal(items) {
479481
this.localItems = (items && items.length > 0) ? items.slice() : [];
480482
this.$emit('refreshed');
481-
this.$root.$emit('table::refreshed', this.id);
483+
this.emitOnRoot('table::refreshed', this.id);
482484
},
483485
_providerUpdate() {
484486
// Refresh the provider items

lib/mixins/dropdown.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import clickOut from './clickout';
2+
import listenOnRootMixin from './listen-on-root'
23
import { from as arrayFrom } from '../utils/array'
34

45
// Determine if an HTML element is visible - Faster than CSS check
@@ -17,6 +18,7 @@ const HEADER_SELECTOR = '.dropdown-header';
1718
const ALL_SELECTOR = [ITEM_SELECTOR, HEADER_SELECTOR].join(',');
1819

1920
export default {
21+
mixins: [listenOnRootMixin],
2022
props: {
2123
id: {
2224
type: String
@@ -51,10 +53,10 @@ export default {
5153
};
5254

5355
// To keep one dropdown opened on page
54-
this.$root.$on('shown::dropdown', listener);
56+
this.listenOnRoot('shown::dropdown', listener);
5557

5658
// Hide when clicked on links
57-
this.$root.$on('clicked::link', listener);
59+
this.listenOnRoot('clicked::link', listener);
5860
},
5961
mounted: clickOut.mounted,
6062
destroyed: clickOut.destroyed,
@@ -65,7 +67,7 @@ export default {
6567
}
6668

6769
if (state) {
68-
this.$root.$emit('shown::dropdown', this);
70+
this.emitOnRoot('shown::dropdown', this);
6971
/*
7072
If this is a touch-enabled device we add extra
7173
empty mouseover listeners to the body's immediate children;
@@ -79,7 +81,7 @@ export default {
7981
});
8082
}
8183
} else {
82-
this.$root.$emit('hidden::dropdown', this);
84+
this.emitOnRoot('hidden::dropdown', this);
8385
/*
8486
If this is a touch-enabled device we remove the extra
8587
empty mouseover listeners we added for iOS support
@@ -108,7 +110,7 @@ export default {
108110
}
109111
if (this.split) {
110112
this.$emit('click', e);
111-
this.$root.$emit('shown::dropdown', this);
113+
this.emitOnRoot('shown::dropdown', this);
112114
} else {
113115
this.toggle();
114116
}

lib/mixins/popover.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Tether from 'tether';
2+
import listenOnRootMixin from './listen-on-root'
23
import { isArray, arrayIncludes } from '../utils/array';
34
import { keys } from '../utils/object';
45

@@ -22,6 +23,7 @@ const TETHER_CLASSES = {
2223
const TRANSITION_DURATION = 150;
2324

2425
export default {
26+
mixins: [listenOnRootMixin],
2527
props: {
2628
constraints: {
2729
type: Array,
@@ -315,10 +317,10 @@ export default {
315317
this.$emit('showChange', newShowState);
316318
if (newShowState) {
317319
this.showPopover();
318-
this.$root.$emit('shown::popover');
320+
this.emitOnRoot('shown::popover');
319321
} else {
320322
this.hidePopover();
321-
this.$root.$emit('hidden::popover');
323+
this.emitOnRoot('hidden::popover');
322324
}
323325
},
324326
/**
@@ -347,7 +349,7 @@ export default {
347349
}
348350
},
349351
created() {
350-
this.$root.$on('hide::popover', () => {
352+
this.listenOnRoot('hide::popover', () => {
351353
this.triggerState = false;
352354
});
353355
},

0 commit comments

Comments
 (0)