Skip to content

unifyObjectStyle

javascript
// 对象形式分发
this.$store.dispatch({
  type: 'foo',
  bar: 'bar'
})
javascript
dispatch (_type, _payload) {
  // check object-style dispatch
  const {
    type,
    payload
  } = unifyObjectStyle(_type, _payload)

  const entry = this._actions[type]
  if (!entry) {
    return
  }

  const result = entry.length > 1
    ? Promise.all(entry.map(handler => handler(payload)))
    : entry[0](payload)
}

const local = {
  dispatch: noNamespace ? store.dispatch : (_type, _payload, _options) => {
    const args = unifyObjectStyle(_type, _payload, _options)
    const { payload, options } = args
    let { type } = args

    if (!options || !options.root) {
      type = namespace + type
      if (__DEV__ && !store._actions[type]) {
        return
      }
    }

    return store.dispatch(type, payload)
  }
}

unifyObjectStyle(
  if (isObject(type) && type.type) {
    option = payload
    payload = type // 规定整个对象都作为 payload 传给 action 或 mutation
    type = type.type
  }
  return { // 统一返回对象
    type,
    payload,
    option
  }
)