eventsMixin.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const defaultEvents = {
  2. onChange() {},
  3. }
  4. export default function eventsMixin(params = { defaultEvents }) {
  5. return Behavior({
  6. lifetimes: {
  7. created () {
  8. this._oriTriggerEvent = this.triggerEvent
  9. this.triggerEvent = this._triggerEvent
  10. },
  11. },
  12. properties: {
  13. events: {
  14. type: Object,
  15. value: defaultEvents,
  16. },
  17. },
  18. data: {
  19. inputEvents: defaultEvents,
  20. },
  21. definitionFilter(defFields) {
  22. // set default data
  23. Object.assign(defFields.data = (defFields.data || {}), {
  24. inputEvents: Object.assign({}, defaultEvents, defFields.inputEvents),
  25. })
  26. // set default methods
  27. Object.assign(defFields.methods = (defFields.methods || {}), {
  28. _triggerEvent(name, params, runCallbacks = true, option) {
  29. const { inputEvents } = this.data
  30. const method = `on${name[0].toUpperCase()}${name.slice(1)}`
  31. const func = inputEvents[method]
  32. if (runCallbacks && typeof func === 'function') {
  33. func.call(this, params)
  34. }
  35. this._oriTriggerEvent(name, params, option)
  36. },
  37. })
  38. // set default observers
  39. Object.assign(defFields.observers = (defFields.observers || {}), {
  40. events(newVal) {
  41. this.setData({
  42. inputEvents: Object.assign({}, defaultEvents, this.data.inputEvents, newVal),
  43. })
  44. },
  45. })
  46. },
  47. })
  48. }