lifecycle.js 2.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  hasOwn
} from 'uni-shared'

import {
  hasLifecycleHook
} from 'uni-helpers/index'

fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14
import {
  callPageHook
} from 'uni-core/service/plugins/util'

import {
  lifecycleMixin
fxy060608's avatar
init v3  
fxy060608 已提交
15
}
fxy060608's avatar
fxy060608 已提交
16 17
  from 'uni-core/service/plugins/lifecycle'

fxy060608's avatar
fxy060608 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import {
  VD_SYNC,
  PAGE_CREATE
} from '../../../constants'

import {
  ON_REACH_BOTTOM_DISTANCE
}
  from '../../constants'

function parsePageCreateOptions (vm, route) {
  const pagePath = '/' + route
  const routeOptions = __uniRoutes.find(route => route.path === pagePath)

  const windowOptions = Object.assign({}, __uniConfig.window, routeOptions.window)
  const disableScroll = windowOptions.disableScroll === true ? 1 : 0
  const onReachBottomDistance = hasOwn(windowOptions, 'onReachBottomDistance')
    ? parseInt(windowOptions.onReachBottomDistance)
    : ON_REACH_BOTTOM_DISTANCE

  const onPageScroll = hasLifecycleHook(vm.$options, 'onPageScroll') ? 1 : 0
  const onPageReachBottom = hasLifecycleHook(vm.$options, 'onReachBottom') ? 1 : 0

  return {
    disableScroll,
    onPageScroll,
    onPageReachBottom,
    onReachBottomDistance
  }
}

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55 56 57
export function initLifecycle (Vue) {
  lifecycleMixin(Vue)

  Vue.mixin({
    beforeCreate () {
      if (this.mpType === 'page') {
        this.$scope = this.$options.pageInstance
        this.$scope.$vm = this
        delete this.$options.pageInstance
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66 67 68 69

        const route = this.$scope.route
        const pageId = this.$scope.$page.id
        // 通知页面已开始创建
        UniServiceJSBridge.publishHandler(VD_SYNC, {
          data: [
            [PAGE_CREATE, [pageId, route, parsePageCreateOptions(this, route)]]
          ],
          options: {
            timestamp: Date.now()
          }
        }, [pageId])
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      }
    },
    created () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onLoad', this.$options.pageQuery)
        callPageHook(this.$scope, 'onShow')
      }
    },
    beforeDestroy () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onUnload')
      }
    },
    mounted () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onReady')
      }
    }
  })
}