Pārlūkot izejas kodu

Add some js Doc for helper (#1100)

*  Add some js Doc for helper

*  Add some js Doc for mapxxxx function

*  Fix some typo and parameters in comments
Lan 7 gadi atpakaļ
vecāks
revīzija
75edfc5322
1 mainītis faili ar 51 papildinājumiem un 0 dzēšanām
  1. 51 0
      src/helpers.js

+ 51 - 0
src/helpers.js

@@ -1,3 +1,9 @@
+/**
+ * Reduce the code which written in Vue.js for getting the state.
+ * @param {String} [namespace] - Module's namespace
+ * @param {Object|Array} states # Object's item can be a function which accept state and getters for param, you can do something for state and getters in it.
+ * @param {Object}
+ */
 export const mapState = normalizeNamespace((namespace, states) => {
 export const mapState = normalizeNamespace((namespace, states) => {
   const res = {}
   const res = {}
   normalizeMap(states).forEach(({ key, val }) => {
   normalizeMap(states).forEach(({ key, val }) => {
@@ -22,10 +28,17 @@ export const mapState = normalizeNamespace((namespace, states) => {
   return res
   return res
 })
 })
 
 
+/**
+ * Reduce the code which written in Vue.js for committing the mutation
+ * @param {String} [namespace] - Module's namespace
+ * @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept anthor params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
+ * @return {Object}
+ */
 export const mapMutations = normalizeNamespace((namespace, mutations) => {
 export const mapMutations = normalizeNamespace((namespace, mutations) => {
   const res = {}
   const res = {}
   normalizeMap(mutations).forEach(({ key, val }) => {
   normalizeMap(mutations).forEach(({ key, val }) => {
     res[key] = function mappedMutation (...args) {
     res[key] = function mappedMutation (...args) {
+      // Get the commit method from store
       let commit = this.$store.commit
       let commit = this.$store.commit
       if (namespace) {
       if (namespace) {
         const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
         const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
@@ -42,9 +55,16 @@ export const mapMutations = normalizeNamespace((namespace, mutations) => {
   return res
   return res
 })
 })
 
 
+/**
+ * Reduce the code which written in Vue.js for getting the getters
+ * @param {String} [namespace] - Module's namespace
+ * @param {Object|Array} getters
+ * @return {Object}
+ */
 export const mapGetters = normalizeNamespace((namespace, getters) => {
 export const mapGetters = normalizeNamespace((namespace, getters) => {
   const res = {}
   const res = {}
   normalizeMap(getters).forEach(({ key, val }) => {
   normalizeMap(getters).forEach(({ key, val }) => {
+    // thie namespace has been mutate by normalizeNamespace
     val = namespace + val
     val = namespace + val
     res[key] = function mappedGetter () {
     res[key] = function mappedGetter () {
       if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
       if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
@@ -62,10 +82,17 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
   return res
   return res
 })
 })
 
 
+/**
+ * Reduce the code which written in Vue.js for dispatch the action
+ * @param {String} [namespace] - Module's namespace
+ * @param {Object|Array} actions # Object's item can be a function which accept `dispatch` function as the first param, it can accept anthor params. You can dispatch action and do any other things in this function. specially, You need to pass anthor params from the mapped function.
+ * @return {Object}
+ */
 export const mapActions = normalizeNamespace((namespace, actions) => {
 export const mapActions = normalizeNamespace((namespace, actions) => {
   const res = {}
   const res = {}
   normalizeMap(actions).forEach(({ key, val }) => {
   normalizeMap(actions).forEach(({ key, val }) => {
     res[key] = function mappedAction (...args) {
     res[key] = function mappedAction (...args) {
+      // get dispatch function from store
       let dispatch = this.$store.dispatch
       let dispatch = this.$store.dispatch
       if (namespace) {
       if (namespace) {
         const module = getModuleByNamespace(this.$store, 'mapActions', namespace)
         const module = getModuleByNamespace(this.$store, 'mapActions', namespace)
@@ -82,6 +109,11 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
   return res
   return res
 })
 })
 
 
+/**
+ * Rebinding namespace param for mapXXX function in special scoped, and return them by simple object
+ * @param {String} namespace
+ * @return {Object}
+ */
 export const createNamespacedHelpers = (namespace) => ({
 export const createNamespacedHelpers = (namespace) => ({
   mapState: mapState.bind(null, namespace),
   mapState: mapState.bind(null, namespace),
   mapGetters: mapGetters.bind(null, namespace),
   mapGetters: mapGetters.bind(null, namespace),
@@ -89,12 +121,24 @@ export const createNamespacedHelpers = (namespace) => ({
   mapActions: mapActions.bind(null, namespace)
   mapActions: mapActions.bind(null, namespace)
 })
 })
 
 
+/**
+ * Normalize the map
+ * normalizeMap([1, 2, 3]) => [ { key: 1, val: 1 }, { key: 2, val: 2 }, { key: 3, val: 3 } ]
+ * normalizeMap({a: 1, b: 2, c: 3}) => [ { key: 'a', val: 1 }, { key: 'b', val: 2 }, { key: 'c', val: 3 } ]
+ * @param {Array|Object} map
+ * @return {Object}
+ */
 function normalizeMap (map) {
 function normalizeMap (map) {
   return Array.isArray(map)
   return Array.isArray(map)
     ? map.map(key => ({ key, val: key }))
     ? map.map(key => ({ key, val: key }))
     : Object.keys(map).map(key => ({ key, val: map[key] }))
     : Object.keys(map).map(key => ({ key, val: map[key] }))
 }
 }
 
 
+/**
+ * Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
+ * @param {Function} fn
+ * @return {Function}
+ */
 function normalizeNamespace (fn) {
 function normalizeNamespace (fn) {
   return (namespace, map) => {
   return (namespace, map) => {
     if (typeof namespace !== 'string') {
     if (typeof namespace !== 'string') {
@@ -107,6 +151,13 @@ function normalizeNamespace (fn) {
   }
   }
 }
 }
 
 
+/**
+ * Search a special module from store by namespace. if module not exist, print error message.
+ * @param {Object} store
+ * @param {String} helper
+ * @param {String} namespace
+ * @return {Object}
+ */
 function getModuleByNamespace (store, helper, namespace) {
 function getModuleByNamespace (store, helper, namespace) {
   const module = store._modulesNamespaceMap[namespace]
   const module = store._modulesNamespaceMap[namespace]
   if (process.env.NODE_ENV !== 'production' && !module) {
   if (process.env.NODE_ENV !== 'production' && !module) {