github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/directives/internal/prop.js (about)

     1  // NOTE: the prop internal directive is compiled and linked
     2  // during _initProps(), before the created hook is called.
     3  // The purpose is to make the initial prop values available
     4  // inside `created` hooks and `data` functions.
     5  
     6  import Watcher from '../../watcher'
     7  import config from '../../config'
     8  import { initProp, updateProp } from '../../compiler/compile-props'
     9  
    10  const bindingModes = config._propBindingModes
    11  
    12  export default {
    13  
    14    bind () {
    15      const child = this.vm
    16      const parent = child._context
    17      // passed in from compiler directly
    18      const prop = this.descriptor.prop
    19      const childKey = prop.path
    20      const parentKey = prop.parentPath
    21      const twoWay = prop.mode === bindingModes.TWO_WAY
    22  
    23      const parentWatcher = this.parentWatcher = new Watcher(
    24        parent,
    25        parentKey,
    26        function (val) {
    27          updateProp(child, prop, val)
    28        }, {
    29          twoWay: twoWay,
    30          filters: prop.filters,
    31          // important: props need to be observed on the
    32          // v-for scope if present
    33          scope: this._scope
    34        }
    35      )
    36  
    37      // set the child initial value.
    38      initProp(child, prop, parentWatcher.value)
    39  
    40      // setup two-way binding
    41      if (twoWay) {
    42        // important: defer the child watcher creation until
    43        // the created hook (after data observation)
    44        var self = this
    45        child.$once('pre-hook:created', function () {
    46          self.childWatcher = new Watcher(
    47            child,
    48            childKey,
    49            function (val) {
    50              parentWatcher.set(val)
    51            }, {
    52              // ensure sync upward before parent sync down.
    53              // this is necessary in cases e.g. the child
    54              // mutates a prop array, then replaces it. (#1683)
    55              sync: true
    56            }
    57          )
    58        })
    59      }
    60    },
    61  
    62    unbind () {
    63      this.parentWatcher.teardown()
    64      if (this.childWatcher) {
    65        this.childWatcher.teardown()
    66      }
    67    }
    68  }