collectObservers.js 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const {
  2. observersHandle,
  3. } = require('./observerHandle')
  4. function collectObservers(observersObj, options, param) {
  5. const self = this
  6. for (const key in options.observers) {
  7. if (options.observers.hasOwnProperty(key)) {
  8. const keyArr = key.split(',')
  9. const arr = []
  10. keyArr.forEach((its) => {
  11. its = its.trim()
  12. let attr = {}
  13. if (its.match(/\./)) {
  14. const _attr = its.split('.')
  15. attr = processChildAttr(self.data, _attr)
  16. } else {
  17. attr = self.data[its]
  18. }
  19. arr.push(attr)
  20. })
  21. keyArr.forEach((its) => {
  22. its = its.trim()
  23. observersObj[its] = Object.create(null)
  24. observersObj[its].fn = options.observers[key]
  25. observersObj[its].arr = arr
  26. })
  27. }
  28. }
  29. observersHandle(observersObj, param, self)
  30. }
  31. function processChildAttr(attr, arr) {
  32. let _ = attr
  33. arr.forEach((name) => {
  34. _ = _[name]
  35. })
  36. return _
  37. }
  38. module.exports = collectObservers