baseBehavior.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Simple bind, faster than native
  3. *
  4. * @param {Function} fn
  5. * @param {Object} ctx
  6. * @return {Function}
  7. */
  8. const bind = (fn, ctx) => {
  9. return (...args) => {
  10. return args.length ? fn.apply(ctx, args) : fn.call(ctx)
  11. }
  12. }
  13. /**
  14. * Object assign
  15. */
  16. const assign = (...args) => Object.assign({}, ...args)
  17. export default Behavior({
  18. properties: {
  19. visible: {
  20. type: Boolean,
  21. value: false,
  22. },
  23. },
  24. methods: {
  25. /**
  26. * 合并参数并绑定方法
  27. *
  28. * @param {Object} opts 参数对象
  29. * @param {Object} fns 方法挂载的属性
  30. */
  31. $$mergeOptionsAndBindMethods (opts = {}, fns = this.fns) {
  32. const options = Object.assign({}, opts)
  33. for (const key in options) {
  34. if (options.hasOwnProperty(key) && typeof options[key] === 'function') {
  35. fns[key] = bind(options[key], this)
  36. delete options[key]
  37. }
  38. }
  39. return options
  40. },
  41. /**
  42. * Promise setData
  43. * @param {Array} args 参数对象
  44. */
  45. $$setData (...args) {
  46. const params = assign({}, ...args)
  47. return new Promise((resolve) => {
  48. this.setData(params, resolve)
  49. })
  50. },
  51. /**
  52. * 延迟指定时间执行回调函数
  53. * @param {Function} callback 回调函数
  54. * @param {Number} timeout 延迟时间
  55. */
  56. $$requestAnimationFrame (callback = () => {}, timeout = 1000 / 60) {
  57. return new Promise((resolve) => setTimeout(resolve, timeout)).then(callback)
  58. },
  59. },
  60. /**
  61. * 组件生命周期函数,在组件实例进入页面节点树时执行
  62. */
  63. created () {
  64. this.fns = {}
  65. },
  66. /**
  67. * 组件生命周期函数,在组件实例被从页面节点树移除时执行
  68. */
  69. detached () {
  70. this.fns = {}
  71. },
  72. })