|
1 | 1 | /*!
|
2 |
| - * vue-router v0.7.10 |
| 2 | + * vue-router v0.7.11 |
3 | 3 | * (c) 2016 Evan You
|
4 | 4 | * Released under the MIT License.
|
5 | 5 | */
|
|
911 | 911 | x: window.pageXOffset,
|
912 | 912 | y: window.pageYOffset
|
913 | 913 | }
|
914 |
| - }, ''); |
| 914 | + }, '', location.href); |
915 | 915 | // then push new state
|
916 | 916 | history.pushState({}, '', url);
|
917 | 917 | }
|
|
1236 | 1236 | };
|
1237 | 1237 |
|
1238 | 1238 | if (activateHook) {
|
1239 |
| - transition.callHooks(activateHook, component, afterActivate, { cleanup: cleanup }); |
| 1239 | + transition.callHooks(activateHook, component, afterActivate, { |
| 1240 | + cleanup: cleanup, |
| 1241 | + postActivate: true |
| 1242 | + }); |
1240 | 1243 | } else {
|
1241 | 1244 | afterActivate();
|
1242 | 1245 | }
|
|
1269 | 1272 |
|
1270 | 1273 | function loadData(component, transition, hook, cb, cleanup) {
|
1271 | 1274 | component.$loadingRouteData = true;
|
1272 |
| - transition.callHooks(hook, component, function (data, onError) { |
1273 |
| - // merge data from multiple data hooks |
1274 |
| - if (Array.isArray(data) && data._needMerge) { |
1275 |
| - data = data.reduce(function (res, obj) { |
1276 |
| - if (isPlainObject(obj)) { |
1277 |
| - Object.keys(obj).forEach(function (key) { |
1278 |
| - res[key] = obj[key]; |
1279 |
| - }); |
1280 |
| - } |
1281 |
| - return res; |
1282 |
| - }, Object.create(null)); |
1283 |
| - } |
1284 |
| - // handle promise sugar syntax |
1285 |
| - var promises = []; |
1286 |
| - if (isPlainObject(data)) { |
1287 |
| - Object.keys(data).forEach(function (key) { |
1288 |
| - var val = data[key]; |
1289 |
| - if (isPromise(val)) { |
1290 |
| - promises.push(val.then(function (resolvedVal) { |
1291 |
| - component.$set(key, resolvedVal); |
1292 |
| - })); |
1293 |
| - } else { |
1294 |
| - component.$set(key, val); |
1295 |
| - } |
1296 |
| - }); |
1297 |
| - } |
1298 |
| - if (!promises.length) { |
1299 |
| - component.$loadingRouteData = false; |
1300 |
| - component.$emit('route-data-loaded', component); |
1301 |
| - cb && cb(); |
1302 |
| - } else { |
1303 |
| - promises[0].constructor.all(promises).then(function () { |
1304 |
| - component.$loadingRouteData = false; |
1305 |
| - component.$emit('route-data-loaded', component); |
1306 |
| - cb && cb(); |
1307 |
| - }, onError); |
1308 |
| - } |
| 1275 | + transition.callHooks(hook, component, function () { |
| 1276 | + component.$loadingRouteData = false; |
| 1277 | + component.$emit('route-data-loaded', component); |
| 1278 | + cb && cb(); |
1309 | 1279 | }, {
|
1310 | 1280 | cleanup: cleanup,
|
1311 |
| - expectData: true |
| 1281 | + postActivate: true, |
| 1282 | + processData: function processData(data) { |
| 1283 | + // handle promise sugar syntax |
| 1284 | + var promises = []; |
| 1285 | + if (isPlainObject(data)) { |
| 1286 | + Object.keys(data).forEach(function (key) { |
| 1287 | + var val = data[key]; |
| 1288 | + if (isPromise(val)) { |
| 1289 | + promises.push(val.then(function (resolvedVal) { |
| 1290 | + component.$set(key, resolvedVal); |
| 1291 | + })); |
| 1292 | + } else { |
| 1293 | + component.$set(key, val); |
| 1294 | + } |
| 1295 | + }); |
| 1296 | + } |
| 1297 | + if (promises.length) { |
| 1298 | + return promises[0].constructor.all(promises); |
| 1299 | + } |
| 1300 | + } |
1312 | 1301 | });
|
1313 | 1302 | }
|
1314 | 1303 |
|
|
1518 | 1507 | * @param {Function} [cb]
|
1519 | 1508 | * @param {Object} [options]
|
1520 | 1509 | * - {Boolean} expectBoolean
|
1521 |
| - * - {Boolean} expectData |
| 1510 | + * - {Boolean} postActive |
| 1511 | + * - {Function} processData |
1522 | 1512 | * - {Function} cleanup
|
1523 | 1513 | */
|
1524 | 1514 |
|
|
1527 | 1517 |
|
1528 | 1518 | var _ref$expectBoolean = _ref.expectBoolean;
|
1529 | 1519 | var expectBoolean = _ref$expectBoolean === undefined ? false : _ref$expectBoolean;
|
1530 |
| - var _ref$expectData = _ref.expectData; |
1531 |
| - var expectData = _ref$expectData === undefined ? false : _ref$expectData; |
| 1520 | + var _ref$postActivate = _ref.postActivate; |
| 1521 | + var postActivate = _ref$postActivate === undefined ? false : _ref$postActivate; |
| 1522 | + var processData = _ref.processData; |
1532 | 1523 | var cleanup = _ref.cleanup;
|
1533 | 1524 |
|
1534 | 1525 | var transition = this;
|
|
1542 | 1533 |
|
1543 | 1534 | // handle errors
|
1544 | 1535 | var onError = function onError(err) {
|
1545 |
| - // cleanup indicates an after-activation hook, |
1546 |
| - // so instead of aborting we just let the transition |
1547 |
| - // finish. |
1548 |
| - cleanup ? next() : abort(); |
| 1536 | + postActivate ? next() : abort(); |
1549 | 1537 | if (err && !transition.router._suppress) {
|
1550 | 1538 | warn('Uncaught error during transition: ');
|
1551 | 1539 | throw err instanceof Error ? err : new Error(err);
|
1552 | 1540 | }
|
1553 | 1541 | };
|
1554 | 1542 |
|
| 1543 | + // since promise swallows errors, we have to |
| 1544 | + // throw it in the next tick... |
| 1545 | + var onPromiseError = function onPromiseError(err) { |
| 1546 | + try { |
| 1547 | + onError(err); |
| 1548 | + } catch (e) { |
| 1549 | + setTimeout(function () { |
| 1550 | + throw e; |
| 1551 | + }, 0); |
| 1552 | + } |
| 1553 | + }; |
| 1554 | + |
1555 | 1555 | // advance the transition to the next step
|
1556 |
| - var next = function next(data) { |
| 1556 | + var next = function next() { |
1557 | 1557 | if (nextCalled) {
|
1558 | 1558 | warn('transition.next() should be called only once.');
|
1559 | 1559 | return;
|
|
1563 | 1563 | cleanup && cleanup();
|
1564 | 1564 | return;
|
1565 | 1565 | }
|
1566 |
| - cb && cb(data, onError); |
| 1566 | + cb && cb(); |
| 1567 | + }; |
| 1568 | + |
| 1569 | + var nextWithBoolean = function nextWithBoolean(res) { |
| 1570 | + if (typeof res === 'boolean') { |
| 1571 | + res ? next() : abort(); |
| 1572 | + } else if (isPromise(res)) { |
| 1573 | + res.then(function (ok) { |
| 1574 | + ok ? next() : abort(); |
| 1575 | + }, onPromiseError); |
| 1576 | + } else if (!hook.length) { |
| 1577 | + next(); |
| 1578 | + } |
| 1579 | + }; |
| 1580 | + |
| 1581 | + var nextWithData = function nextWithData(data) { |
| 1582 | + var res = undefined; |
| 1583 | + try { |
| 1584 | + res = processData(data); |
| 1585 | + } catch (err) { |
| 1586 | + return onError(err); |
| 1587 | + } |
| 1588 | + if (isPromise(res)) { |
| 1589 | + res.then(next, onPromiseError); |
| 1590 | + } else { |
| 1591 | + next(); |
| 1592 | + } |
1567 | 1593 | };
|
1568 | 1594 |
|
1569 | 1595 | // expose a clone of the transition object, so that each
|
|
1573 | 1599 | to: transition.to,
|
1574 | 1600 | from: transition.from,
|
1575 | 1601 | abort: abort,
|
1576 |
| - next: next, |
| 1602 | + next: processData ? nextWithData : next, |
1577 | 1603 | redirect: function redirect() {
|
1578 | 1604 | transition.redirect.apply(transition, arguments);
|
1579 | 1605 | }
|
|
1587 | 1613 | return onError(err);
|
1588 | 1614 | }
|
1589 | 1615 |
|
1590 |
| - // handle boolean/promise return values |
1591 |
| - var resIsPromise = isPromise(res); |
1592 | 1616 | if (expectBoolean) {
|
1593 |
| - if (typeof res === 'boolean') { |
1594 |
| - res ? next() : abort(); |
1595 |
| - } else if (resIsPromise) { |
1596 |
| - res.then(function (ok) { |
1597 |
| - ok ? next() : abort(); |
1598 |
| - }, onError); |
1599 |
| - } else if (!hook.length) { |
1600 |
| - next(res); |
| 1617 | + // boolean hooks |
| 1618 | + nextWithBoolean(res); |
| 1619 | + } else if (isPromise(res)) { |
| 1620 | + // promise |
| 1621 | + if (processData) { |
| 1622 | + res.then(nextWithData, onPromiseError); |
| 1623 | + } else { |
| 1624 | + res.then(next, onPromiseError); |
1601 | 1625 | }
|
1602 |
| - } else if (resIsPromise) { |
1603 |
| - res.then(next, onError); |
1604 |
| - } else if (expectData && isPlainOjbect(res) || !hook.length) { |
1605 |
| - next(res); |
| 1626 | + } else if (processData && isPlainOjbect(res)) { |
| 1627 | + // data promise sugar |
| 1628 | + nextWithData(res); |
| 1629 | + } else if (!hook.length) { |
| 1630 | + next(); |
1606 | 1631 | }
|
1607 | 1632 | };
|
1608 | 1633 |
|
|
1619 | 1644 | var _this = this;
|
1620 | 1645 |
|
1621 | 1646 | if (Array.isArray(hooks)) {
|
1622 |
| - (function () { |
1623 |
| - var res = []; |
1624 |
| - res._needMerge = true; |
1625 |
| - var onError = undefined; |
1626 |
| - _this.runQueue(hooks, function (hook, _, next) { |
1627 |
| - if (!_this.aborted) { |
1628 |
| - _this.callHook(hook, context, function (r, onError) { |
1629 |
| - if (r) res.push(r); |
1630 |
| - onError = onError; |
1631 |
| - next(); |
1632 |
| - }, options); |
1633 |
| - } |
1634 |
| - }, function () { |
1635 |
| - cb(res, onError); |
1636 |
| - }); |
1637 |
| - })(); |
| 1647 | + this.runQueue(hooks, function (hook, _, next) { |
| 1648 | + if (!_this.aborted) { |
| 1649 | + _this.callHook(hook, context, next, options); |
| 1650 | + } |
| 1651 | + }, cb); |
1638 | 1652 | } else {
|
1639 | 1653 | this.callHook(hooks, context, cb, options);
|
1640 | 1654 | }
|
|
1884 | 1898 | return;
|
1885 | 1899 | }
|
1886 | 1900 | // handle click
|
1887 |
| - this.el.addEventListener('click', _bind(this.onClick, this)); |
| 1901 | + this.handler = _bind(this.onClick, this); |
| 1902 | + this.el.addEventListener('click', this.handler); |
1888 | 1903 | },
|
1889 | 1904 |
|
1890 | 1905 | update: function update(target) {
|
|
0 commit comments