|
1 | 1 | /*! |
2 | | - * Vuex v0.6.2 |
| 2 | + * Vuex v0.6.3 |
3 | 3 | * (c) 2016 Evan You |
4 | 4 | * Released under the MIT License. |
5 | 5 | */ |
|
158 | 158 | _init.call(this, options); |
159 | 159 | }; |
160 | 160 |
|
| 161 | + /** |
| 162 | + * Vuex init hook, injected into each instances init hooks list. |
| 163 | + */ |
| 164 | + |
161 | 165 | function vuexInit() { |
162 | 166 | var options = this.$options; |
163 | 167 | var store = options.store; |
|
194 | 198 | if (actions) { |
195 | 199 | options.methods = options.methods || {}; |
196 | 200 | for (var _key in actions) { |
197 | | - options.methods[_key] = makeBoundAction(actions[_key], this.$store); |
| 201 | + options.methods[_key] = makeBoundAction(this.$store, actions[_key], _key); |
198 | 202 | } |
199 | 203 | } |
200 | 204 | } |
201 | 205 | } |
202 | 206 |
|
| 207 | + /** |
| 208 | + * Setter for all getter properties. |
| 209 | + */ |
| 210 | + |
203 | 211 | function setter() { |
204 | 212 | throw new Error('vuex getter properties are read-only.'); |
205 | 213 | } |
206 | 214 |
|
| 215 | + /** |
| 216 | + * Define a Vuex getter on an instance. |
| 217 | + * |
| 218 | + * @param {Vue} vm |
| 219 | + * @param {String} key |
| 220 | + * @param {Function} getter |
| 221 | + */ |
| 222 | + |
207 | 223 | function defineVuexGetter(vm, key, getter) { |
208 | | - Object.defineProperty(vm, key, { |
209 | | - enumerable: true, |
210 | | - configurable: true, |
211 | | - get: makeComputedGetter(vm.$store, getter), |
212 | | - set: setter |
213 | | - }); |
| 224 | + if (typeof getter !== 'function') { |
| 225 | + console.warn('[vuex] Getter bound to key \'vuex.getters.' + key + '\' is not a function.'); |
| 226 | + } else { |
| 227 | + Object.defineProperty(vm, key, { |
| 228 | + enumerable: true, |
| 229 | + configurable: true, |
| 230 | + get: makeComputedGetter(vm.$store, getter), |
| 231 | + set: setter |
| 232 | + }); |
| 233 | + } |
214 | 234 | } |
215 | 235 |
|
| 236 | + /** |
| 237 | + * Make a computed getter, using the same caching mechanism of computed |
| 238 | + * properties. In addition, it is cached on the raw getter function using |
| 239 | + * the store's unique cache id. This makes the same getter shared |
| 240 | + * across all components use the same underlying watcher, and makes |
| 241 | + * the getter evaluated only once during every flush. |
| 242 | + * |
| 243 | + * @param {Store} store |
| 244 | + * @param {Function} getter |
| 245 | + */ |
| 246 | + |
216 | 247 | function makeComputedGetter(store, getter) { |
217 | 248 | var id = store._getterCacheId; |
| 249 | + |
218 | 250 | // cached |
219 | 251 | if (getter[id]) { |
220 | 252 | return getter[id]; |
|
238 | 270 | return computedGetter; |
239 | 271 | } |
240 | 272 |
|
241 | | - function makeBoundAction(action, store) { |
| 273 | + /** |
| 274 | + * Make a bound-to-store version of a raw action function. |
| 275 | + * |
| 276 | + * @param {Store} store |
| 277 | + * @param {Function} action |
| 278 | + * @param {String} key |
| 279 | + */ |
| 280 | + |
| 281 | + function makeBoundAction(store, action, key) { |
| 282 | + if (typeof action !== 'function') { |
| 283 | + console.warn('[vuex] Action bound to key \'vuex.actions.' + key + '\' is not a function.'); |
| 284 | + } |
242 | 285 | return function vuexBoundAction() { |
243 | 286 | for (var _len = arguments.length, args = Array(_len), _key2 = 0; _key2 < _len; _key2++) { |
244 | 287 | args[_key2] = arguments[_key2]; |
|
344 | 387 | */ |
345 | 388 |
|
346 | 389 | value: function dispatch(type) { |
347 | | - var _this2 = this; |
348 | | - |
349 | 390 | for (var _len2 = arguments.length, payload = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { |
350 | 391 | payload[_key2 - 1] = arguments[_key2]; |
351 | 392 | } |
352 | 393 |
|
| 394 | + var silent = false; |
353 | 395 | // compatibility for object actions, e.g. FSA |
354 | 396 | if ((typeof type === 'undefined' ? 'undefined' : babelHelpers.typeof(type)) === 'object' && type.type && arguments.length === 1) { |
355 | | - payload = [type]; |
| 397 | + payload = [type.payload]; |
| 398 | + if (type.silent) silent = true; |
356 | 399 | type = type.type; |
357 | 400 | } |
358 | 401 | var mutation = this._mutations[type]; |
359 | | - var prevSnapshot = this._prevSnapshot; |
360 | 402 | var state = this.state; |
361 | | - var snapshot = void 0, |
362 | | - clonedPayload = void 0; |
363 | 403 | if (mutation) { |
364 | 404 | this._dispatching = true; |
365 | 405 | // apply the mutation |
|
371 | 411 | mutation.apply(undefined, [state].concat(babelHelpers.toConsumableArray(payload))); |
372 | 412 | } |
373 | 413 | this._dispatching = false; |
374 | | - // invoke middlewares |
375 | | - if (this._needSnapshots) { |
376 | | - snapshot = this._prevSnapshot = deepClone(state); |
377 | | - clonedPayload = deepClone(payload); |
378 | | - } |
379 | | - this._middlewares.forEach(function (m) { |
380 | | - if (m.onMutation) { |
381 | | - if (m.snapshot) { |
382 | | - m.onMutation({ type: type, payload: clonedPayload }, snapshot, prevSnapshot, _this2); |
383 | | - } else { |
384 | | - m.onMutation({ type: type, payload: payload }, state, _this2); |
385 | | - } |
386 | | - } |
387 | | - }); |
| 414 | + if (!silent) this._applyMiddlewares(type, payload); |
388 | 415 | } else { |
389 | 416 | console.warn('[vuex] Unknown mutation: ' + type); |
390 | 417 | } |
|
403 | 430 | }, { |
404 | 431 | key: 'watch', |
405 | 432 | value: function watch(expOrFn, cb, options) { |
406 | | - var _this3 = this; |
| 433 | + var _this2 = this; |
407 | 434 |
|
408 | 435 | return this._vm.$watch(function () { |
409 | | - return typeof expOrFn === 'function' ? expOrFn(_this3.state) : _this3._vm.$get(expOrFn); |
| 436 | + return typeof expOrFn === 'function' ? expOrFn(_this2.state) : _this2._vm.$get(expOrFn); |
410 | 437 | }, cb, options); |
411 | 438 | } |
412 | 439 |
|
|
440 | 467 | }, { |
441 | 468 | key: '_setupModuleState', |
442 | 469 | value: function _setupModuleState(state, modules) { |
443 | | - var setPath = Vue.parsers.path.setPath; |
444 | | - |
445 | 470 | Object.keys(modules).forEach(function (key) { |
446 | | - setPath(state, key, modules[key].state || {}); |
| 471 | + Vue.set(state, key, modules[key].state || {}); |
447 | 472 | }); |
448 | 473 | } |
449 | 474 |
|
|
458 | 483 | key: '_setupModuleMutations', |
459 | 484 | value: function _setupModuleMutations(updatedModules) { |
460 | 485 | var modules = this._modules; |
461 | | - var getPath = Vue.parsers.path.getPath; |
462 | | - |
463 | 486 | var allMutations = [this._rootMutations]; |
464 | 487 | Object.keys(updatedModules).forEach(function (key) { |
465 | 488 | modules[key] = updatedModules[key]; |
|
476 | 499 | args[_key3 - 1] = arguments[_key3]; |
477 | 500 | } |
478 | 501 |
|
479 | | - original.apply(undefined, [getPath(state, key)].concat(args)); |
| 502 | + original.apply(undefined, [state[key]].concat(args)); |
480 | 503 | }; |
481 | 504 | }); |
482 | 505 | allMutations.push(mutations); |
|
496 | 519 | }, { |
497 | 520 | key: '_setupMutationCheck', |
498 | 521 | value: function _setupMutationCheck() { |
499 | | - var _this4 = this; |
| 522 | + var _this3 = this; |
500 | 523 |
|
501 | 524 | var Watcher = getWatcher(this._vm); |
502 | 525 | /* eslint-disable no-new */ |
503 | 526 | new Watcher(this._vm, '$data', function () { |
504 | | - if (!_this4._dispatching) { |
| 527 | + if (!_this3._dispatching) { |
505 | 528 | throw new Error('[vuex] Do not mutate vuex store state outside mutation handlers.'); |
506 | 529 | } |
507 | 530 | }, { deep: true, sync: true }); |
|
522 | 545 | }, { |
523 | 546 | key: '_setupMiddlewares', |
524 | 547 | value: function _setupMiddlewares(middlewares, state) { |
525 | | - var _this5 = this; |
| 548 | + var _this4 = this; |
526 | 549 |
|
527 | 550 | this._middlewares = [devtoolMiddleware].concat(middlewares); |
528 | 551 | this._needSnapshots = middlewares.some(function (m) { |
|
535 | 558 | // call init hooks |
536 | 559 | this._middlewares.forEach(function (m) { |
537 | 560 | if (m.onInit) { |
538 | | - m.onInit(m.snapshot ? initialSnapshot : state, _this5); |
| 561 | + m.onInit(m.snapshot ? initialSnapshot : state, _this4); |
| 562 | + } |
| 563 | + }); |
| 564 | + } |
| 565 | + |
| 566 | + /** |
| 567 | + * Apply the middlewares on a given mutation. |
| 568 | + * |
| 569 | + * @param {String} type |
| 570 | + * @param {Array} payload |
| 571 | + */ |
| 572 | + |
| 573 | + }, { |
| 574 | + key: '_applyMiddlewares', |
| 575 | + value: function _applyMiddlewares(type, payload) { |
| 576 | + var _this5 = this; |
| 577 | + |
| 578 | + var state = this.state; |
| 579 | + var prevSnapshot = this._prevSnapshot; |
| 580 | + var snapshot = void 0, |
| 581 | + clonedPayload = void 0; |
| 582 | + if (this._needSnapshots) { |
| 583 | + snapshot = this._prevSnapshot = deepClone(state); |
| 584 | + clonedPayload = deepClone(payload); |
| 585 | + } |
| 586 | + this._middlewares.forEach(function (m) { |
| 587 | + if (m.onMutation) { |
| 588 | + if (m.snapshot) { |
| 589 | + m.onMutation({ type: type, payload: clonedPayload }, snapshot, prevSnapshot, _this5); |
| 590 | + } else { |
| 591 | + m.onMutation({ type: type, payload: payload }, state, _this5); |
| 592 | + } |
539 | 593 | } |
540 | 594 | }); |
541 | 595 | } |
|
552 | 606 | }(); |
553 | 607 |
|
554 | 608 | function install(_Vue) { |
| 609 | + if (Vue) { |
| 610 | + console.warn('[vuex] already installed. Vue.use(Vuex) should be called only once.'); |
| 611 | + return; |
| 612 | + } |
555 | 613 | Vue = _Vue; |
556 | 614 | override(Vue); |
557 | 615 | } |
|
0 commit comments