| 2866 | } |
| 2867 | |
| 2868 | function stateMixin (Vue) { |
| 2869 | // flow somehow has problems with directly declared definition object |
| 2870 | // when using Object.defineProperty, so we have to procedurally build up |
| 2871 | // the object here. |
| 2872 | var dataDef = {}; |
| 2873 | dataDef.get = function () { return this._data }; |
| 2874 | var propsDef = {}; |
| 2875 | propsDef.get = function () { return this._props }; |
| 2876 | if (process.env.NODE_ENV !== 'production') { |
| 2877 | dataDef.set = function (newData) { |
| 2878 | warn( |
| 2879 | 'Avoid replacing instance root $data. ' + |
| 2880 | 'Use nested data properties instead.', |
| 2881 | this |
| 2882 | ); |
| 2883 | }; |
| 2884 | propsDef.set = function () { |
| 2885 | warn("$props is readonly.", this); |
| 2886 | }; |
| 2887 | } |
| 2888 | Object.defineProperty(Vue.prototype, '$data', dataDef); |
| 2889 | Object.defineProperty(Vue.prototype, '$props', propsDef); |
| 2890 | |
| 2891 | Vue.prototype.$set = set; |
| 2892 | Vue.prototype.$delete = del; |
| 2893 | |
| 2894 | Vue.prototype.$watch = function ( |
| 2895 | expOrFn, |
| 2896 | cb, |
| 2897 | options |
| 2898 | ) { |
| 2899 | var vm = this; |
| 2900 | options = options || {}; |
| 2901 | options.user = true; |
| 2902 | var watcher = new Watcher(vm, expOrFn, cb, options); |
| 2903 | if (options.immediate) { |
| 2904 | cb.call(vm, watcher.value); |
| 2905 | } |
| 2906 | return function unwatchFn () { |
| 2907 | watcher.teardown(); |
| 2908 | } |
| 2909 | }; |
| 2910 | } |
| 2911 | |
| 2912 | /* */ |
| 2913 | |