bind-object-props.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* @flow */
  2. import config from 'core/config'
  3. import {
  4. warn,
  5. isObject,
  6. toObject,
  7. isReservedAttribute
  8. } from 'core/util/index'
  9. /**
  10. * Runtime helper for merging v-bind="object" into a VNode's data.
  11. */
  12. export function bindObjectProps (
  13. data: any,
  14. tag: string,
  15. value: any,
  16. asProp?: boolean
  17. ): VNodeData {
  18. if (value) {
  19. if (!isObject(value)) {
  20. process.env.NODE_ENV !== 'production' && warn(
  21. 'v-bind without argument expects an Object or Array value',
  22. this
  23. )
  24. } else {
  25. if (Array.isArray(value)) {
  26. value = toObject(value)
  27. }
  28. let hash
  29. for (const key in value) {
  30. if (
  31. key === 'class' ||
  32. key === 'style' ||
  33. isReservedAttribute(key)
  34. ) {
  35. hash = data
  36. } else {
  37. const type = data.attrs && data.attrs.type
  38. hash = asProp || config.mustUseProp(tag, type, key)
  39. ? data.domProps || (data.domProps = {})
  40. : data.attrs || (data.attrs = {})
  41. }
  42. if (!(key in hash)) {
  43. hash[key] = value[key]
  44. }
  45. }
  46. }
  47. }
  48. return data
  49. }