index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. relation: {
  4. name: 'badge',
  5. type: 'descendant',
  6. linked(target) {
  7. this.badges.push(target);
  8. this.setActive(this.data.active);
  9. },
  10. unlinked(target) {
  11. this.badges = this.badges.filter(item => item !== target);
  12. this.setActive(this.data.active);
  13. }
  14. },
  15. props: {
  16. active: {
  17. type: Number,
  18. value: 0,
  19. observer: 'setActive'
  20. }
  21. },
  22. beforeCreate() {
  23. this.badges = [];
  24. this.currentActive = -1;
  25. },
  26. methods: {
  27. setActive(active) {
  28. const { badges, currentActive } = this;
  29. if (!badges.length) {
  30. return Promise.resolve();
  31. }
  32. this.currentActive = active;
  33. const stack = [];
  34. if (currentActive !== active && badges[currentActive]) {
  35. stack.push(badges[currentActive].setActive(false));
  36. }
  37. if (badges[active]) {
  38. stack.push(badges[active].setActive(true));
  39. }
  40. return Promise.all(stack);
  41. }
  42. }
  43. });