|
1 | 1 | /*!
|
2 |
| - * Vuex v2.0.0-rc.2 |
| 2 | + * Vuex v2.0.0-rc.3 |
3 | 3 | * (c) 2016 Evan You
|
4 | 4 | * Released under the MIT License.
|
5 | 5 | */
|
|
67 | 67 | Object.keys(map).forEach(function (key) {
|
68 | 68 | var fn = map[key];
|
69 | 69 | res[key] = function mappedState() {
|
70 |
| - return fn.call(this, this.$store.state); |
| 70 | + return fn.call(this, this.$store.state, this.$store.getters); |
71 | 71 | };
|
72 | 72 | });
|
73 | 73 | return res;
|
|
298 | 298 | var res = handler({
|
299 | 299 | dispatch: dispatch,
|
300 | 300 | commit: commit,
|
| 301 | + getters: store.getters, |
301 | 302 | state: getNestedState(store.state, path),
|
302 | 303 | rootState: store.state
|
303 | 304 | }, payload, cb);
|
|
455 | 456 | var fn = getters[key];
|
456 | 457 | // use computed to leverage its lazy-caching mechanism
|
457 | 458 | computed[key] = function () {
|
458 |
| - return fn(store._vm.state); |
| 459 | + return fn(store); |
459 | 460 | };
|
460 | 461 | Object.defineProperty(store.getters, key, {
|
461 | 462 | get: function get() {
|
|
481 | 482 | var modules = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
482 | 483 | var path = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
|
483 | 484 |
|
484 |
| - if (!modules) return getters; |
| 485 | + if (!path.length) { |
| 486 | + wrapGetters(getters, getters, path, true); |
| 487 | + } |
| 488 | + if (!modules) { |
| 489 | + return getters; |
| 490 | + } |
485 | 491 | Object.keys(modules).forEach(function (key) {
|
486 | 492 | var module = modules[key];
|
487 | 493 | var modulePath = path.concat(key);
|
488 | 494 | if (module.getters) {
|
489 |
| - Object.keys(module.getters).forEach(function (getterKey) { |
490 |
| - var rawGetter = module.getters[getterKey]; |
491 |
| - if (getters[getterKey]) { |
492 |
| - console.error('[vuex] duplicate getter key: ' + getterKey); |
493 |
| - return; |
494 |
| - } |
495 |
| - getters[getterKey] = function wrappedGetter(state) { |
496 |
| - return rawGetter(getNestedState(state, modulePath), state); |
497 |
| - }; |
498 |
| - }); |
| 495 | + wrapGetters(getters, module.getters, modulePath); |
499 | 496 | }
|
500 | 497 | extractModuleGetters(getters, module.modules, modulePath);
|
501 | 498 | });
|
502 | 499 | return getters;
|
503 | 500 | }
|
504 | 501 |
|
| 502 | + function wrapGetters(getters, moduleGetters, modulePath, force) { |
| 503 | + Object.keys(moduleGetters).forEach(function (getterKey) { |
| 504 | + var rawGetter = moduleGetters[getterKey]; |
| 505 | + if (getters[getterKey] && !force) { |
| 506 | + console.error('[vuex] duplicate getter key: ' + getterKey); |
| 507 | + return; |
| 508 | + } |
| 509 | + getters[getterKey] = function wrappedGetter(store) { |
| 510 | + return rawGetter(getNestedState(store.state, modulePath), // local state |
| 511 | + store.getters, // getters |
| 512 | + store.state // root state |
| 513 | + ); |
| 514 | + }; |
| 515 | + }); |
| 516 | + } |
| 517 | + |
505 | 518 | function enableStrictMode(store) {
|
506 | 519 | store._vm.$watch('state', function () {
|
507 | 520 | assert(store._committing, 'Do not mutate vuex store state outside mutation handlers.');
|
|
517 | 530 | }
|
518 | 531 |
|
519 | 532 | function getNestedState(state, path) {
|
520 |
| - return path.reduce(function (state, key) { |
| 533 | + return path.length ? path.reduce(function (state, key) { |
521 | 534 | return state[key];
|
522 |
| - }, state); |
| 535 | + }, state) : state; |
523 | 536 | }
|
524 | 537 |
|
525 | 538 | function install(_Vue) {
|
|
0 commit comments