|
1 | 1 | /*! |
2 | | - * Vuex v0.6.3 |
| 2 | + * Vuex v0.8.2 |
3 | 3 | * (c) 2016 Evan You |
4 | 4 | * Released under the MIT License. |
5 | 5 | */ |
|
9 | 9 | (global.Vuex = factory()); |
10 | 10 | }(this, function () { 'use strict'; |
11 | 11 |
|
12 | | - var babelHelpers = {}; |
13 | | - babelHelpers.typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
| 12 | + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
14 | 13 | return typeof obj; |
15 | 14 | } : function (obj) { |
16 | 15 | return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; |
17 | 16 | }; |
18 | 17 |
|
19 | | - babelHelpers.classCallCheck = function (instance, Constructor) { |
| 18 | + var classCallCheck = function (instance, Constructor) { |
20 | 19 | if (!(instance instanceof Constructor)) { |
21 | 20 | throw new TypeError("Cannot call a class as a function"); |
22 | 21 | } |
23 | 22 | }; |
24 | 23 |
|
25 | | - babelHelpers.createClass = function () { |
| 24 | + var createClass = function () { |
26 | 25 | function defineProperties(target, props) { |
27 | 26 | for (var i = 0; i < props.length; i++) { |
28 | 27 | var descriptor = props[i]; |
|
40 | 39 | }; |
41 | 40 | }(); |
42 | 41 |
|
43 | | - babelHelpers.toConsumableArray = function (arr) { |
| 42 | + var toConsumableArray = function (arr) { |
44 | 43 | if (Array.isArray(arr)) { |
45 | 44 | for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; |
46 | 45 |
|
|
50 | 49 | } |
51 | 50 | }; |
52 | 51 |
|
53 | | - babelHelpers; |
54 | | - |
55 | 52 | /** |
56 | 53 | * Merge an array of objects into one. |
57 | 54 | * |
|
89 | 86 | function deepClone(obj) { |
90 | 87 | if (Array.isArray(obj)) { |
91 | 88 | return obj.map(deepClone); |
92 | | - } else if (obj && (typeof obj === 'undefined' ? 'undefined' : babelHelpers.typeof(obj)) === 'object') { |
| 89 | + } else if (obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object') { |
93 | 90 | var cloned = {}; |
94 | 91 | var keys = Object.keys(obj); |
95 | 92 | for (var i = 0, l = keys.length; i < l; i++) { |
|
110 | 107 | var Watcher = void 0; |
111 | 108 | function getWatcher(vm) { |
112 | 109 | if (!Watcher) { |
113 | | - var unwatch = vm.$watch('__vuex__', function (a) { |
114 | | - return a; |
115 | | - }); |
| 110 | + var noop = function noop() {}; |
| 111 | + var unwatch = vm.$watch(noop, noop); |
116 | 112 | Watcher = vm._watchers[0].constructor; |
117 | 113 | unwatch(); |
118 | 114 | } |
|
134 | 130 | if (!hook) return; |
135 | 131 | hook.emit('vuex:init', store); |
136 | 132 | hook.on('vuex:travel-to-state', function (targetState) { |
137 | | - var currentState = store._vm._data; |
138 | 133 | store._dispatching = true; |
139 | | - Object.keys(targetState).forEach(function (key) { |
140 | | - currentState[key] = targetState[key]; |
141 | | - }); |
| 134 | + store._vm.state = targetState; |
142 | 135 | store._dispatching = false; |
143 | 136 | }); |
144 | 137 | }, |
|
149 | 142 | }; |
150 | 143 |
|
151 | 144 | function override (Vue) { |
152 | | - // override init and inject vuex init procedure |
153 | | - var _init = Vue.prototype._init; |
154 | | - Vue.prototype._init = function () { |
155 | | - var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 145 | + var version = Number(Vue.version.split('.')[0]); |
156 | 146 |
|
157 | | - options.init = options.init ? [vuexInit].concat(options.init) : vuexInit; |
158 | | - _init.call(this, options); |
159 | | - }; |
| 147 | + if (version >= 2) { |
| 148 | + var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1; |
| 149 | + Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit }); |
| 150 | + } else { |
| 151 | + (function () { |
| 152 | + // override init and inject vuex init procedure |
| 153 | + // for 1.x backwards compatibility. |
| 154 | + var _init = Vue.prototype._init; |
| 155 | + Vue.prototype._init = function () { |
| 156 | + var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; |
| 157 | + |
| 158 | + options.init = options.init ? [vuexInit].concat(options.init) : vuexInit; |
| 159 | + _init.call(this, options); |
| 160 | + }; |
| 161 | + })(); |
| 162 | + } |
160 | 163 |
|
161 | 164 | /** |
162 | 165 | * Vuex init hook, injected into each instances init hooks list. |
|
179 | 182 | console.warn('[vuex] store not injected. make sure to ' + 'provide the store option in your root component.'); |
180 | 183 | } |
181 | 184 | var state = vuex.state; |
182 | | - var getters = vuex.getters; |
183 | 185 | var actions = vuex.actions; |
| 186 | + var getters = vuex.getters; |
184 | 187 | // handle deprecated state option |
185 | 188 |
|
186 | 189 | if (state && !getters) { |
|
254 | 257 | var vm = store._vm; |
255 | 258 | var Watcher = getWatcher(vm); |
256 | 259 | var Dep = getDep(vm); |
257 | | - var watcher = new Watcher(vm, function (state) { |
258 | | - return getter(state); |
| 260 | + var watcher = new Watcher(vm, function (vm) { |
| 261 | + return getter(vm.state); |
259 | 262 | }, null, { lazy: true }); |
260 | 263 | var computedGetter = function computedGetter() { |
261 | 264 | if (watcher.dirty) { |
|
333 | 336 | var middlewares = _ref$middlewares === undefined ? [] : _ref$middlewares; |
334 | 337 | var _ref$strict = _ref.strict; |
335 | 338 | var strict = _ref$strict === undefined ? false : _ref$strict; |
336 | | - babelHelpers.classCallCheck(this, Store); |
| 339 | + classCallCheck(this, Store); |
337 | 340 |
|
338 | 341 | this._getterCacheId = 'vuex_store_' + uid++; |
339 | 342 | this._dispatching = false; |
|
357 | 360 | var silent = Vue.config.silent; |
358 | 361 | Vue.config.silent = true; |
359 | 362 | this._vm = new Vue({ |
360 | | - data: state |
| 363 | + data: { |
| 364 | + state: state |
| 365 | + } |
361 | 366 | }); |
362 | 367 | Vue.config.silent = silent; |
363 | 368 | this._setupModuleState(state, modules); |
|
376 | 381 | * @return {Object} |
377 | 382 | */ |
378 | 383 |
|
379 | | - babelHelpers.createClass(Store, [{ |
| 384 | + createClass(Store, [{ |
380 | 385 | key: 'dispatch', |
381 | 386 |
|
382 | 387 |
|
|
393 | 398 |
|
394 | 399 | var silent = false; |
395 | 400 | // compatibility for object actions, e.g. FSA |
396 | | - if ((typeof type === 'undefined' ? 'undefined' : babelHelpers.typeof(type)) === 'object' && type.type && arguments.length === 1) { |
| 401 | + if ((typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'object' && type.type && arguments.length === 1) { |
397 | 402 | payload = [type.payload]; |
398 | 403 | if (type.silent) silent = true; |
399 | 404 | type = type.type; |
|
405 | 410 | // apply the mutation |
406 | 411 | if (Array.isArray(mutation)) { |
407 | 412 | mutation.forEach(function (m) { |
408 | | - return m.apply(undefined, [state].concat(babelHelpers.toConsumableArray(payload))); |
| 413 | + return m.apply(undefined, [state].concat(toConsumableArray(payload))); |
409 | 414 | }); |
410 | 415 | } else { |
411 | | - mutation.apply(undefined, [state].concat(babelHelpers.toConsumableArray(payload))); |
| 416 | + mutation.apply(undefined, [state].concat(toConsumableArray(payload))); |
412 | 417 | } |
413 | 418 | this._dispatching = false; |
414 | 419 | if (!silent) this._applyMiddlewares(type, payload); |
|
422 | 427 | * Same API as Vue's $watch, except when watching a function, |
423 | 428 | * the function gets the state as the first argument. |
424 | 429 | * |
425 | | - * @param {String|Function} expOrFn |
| 430 | + * @param {Function} fn |
426 | 431 | * @param {Function} cb |
427 | 432 | * @param {Object} [options] |
428 | 433 | */ |
429 | 434 |
|
430 | 435 | }, { |
431 | 436 | key: 'watch', |
432 | | - value: function watch(expOrFn, cb, options) { |
| 437 | + value: function watch(fn, cb, options) { |
433 | 438 | var _this2 = this; |
434 | 439 |
|
| 440 | + if (typeof fn !== 'function') { |
| 441 | + console.error('Vuex store.watch only accepts function.'); |
| 442 | + return; |
| 443 | + } |
435 | 444 | return this._vm.$watch(function () { |
436 | | - return typeof expOrFn === 'function' ? expOrFn(_this2.state) : _this2._vm.$get(expOrFn); |
| 445 | + return fn(_this2.state); |
437 | 446 | }, cb, options); |
438 | 447 | } |
439 | 448 |
|
|
523 | 532 |
|
524 | 533 | var Watcher = getWatcher(this._vm); |
525 | 534 | /* eslint-disable no-new */ |
526 | | - new Watcher(this._vm, '$data', function () { |
| 535 | + new Watcher(this._vm, 'state', function () { |
527 | 536 | if (!_this3._dispatching) { |
528 | 537 | throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.'); |
529 | 538 | } |
|
596 | 605 | }, { |
597 | 606 | key: 'state', |
598 | 607 | get: function get() { |
599 | | - return this._vm._data; |
| 608 | + return this._vm.state; |
600 | 609 | }, |
601 | 610 | set: function set(v) { |
602 | 611 | throw new Error('[vuex] Vuex root state is read only.'); |
|
619 | 628 | install(window.Vue); |
620 | 629 | } |
621 | 630 |
|
622 | | - function createLogger() { |
623 | | - console.warn('[vuex] Vuex.createLogger has been deprecated.' + 'Use `import createLogger from \'vuex/logger\' instead.'); |
624 | | - } |
625 | | - |
626 | 631 | var index = { |
627 | 632 | Store: Store, |
628 | | - install: install, |
629 | | - createLogger: createLogger |
| 633 | + install: install |
630 | 634 | }; |
631 | 635 |
|
632 | 636 | return index; |
|
0 commit comments