From aa0e831e89c713bb430d24f45eb2d561cf50c961 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sat, 23 Jun 2018 00:47:15 +0900 Subject: [PATCH] :boom: breaking(extend): fix this context binding closes #306 closes #286 closes #260 NOTE: revert #260 issue, back to revert. --- src/extend.js | 67 +++++++++++++++------------------------- src/install.js | 4 --- test/unit/issues.test.js | 29 ----------------- 3 files changed, 25 insertions(+), 75 deletions(-) diff --git a/src/extend.js b/src/extend.js index b86bdfbe2..b71cb63c5 100644 --- a/src/extend.js +++ b/src/extend.js @@ -1,47 +1,30 @@ /* @flow */ export default function extend (Vue: any): void { - // $FlowFixMe - Object.defineProperty(Vue.prototype, '$t', { - get () { - return (key: Path, ...values: any): TranslateResult => { - const i18n = this.$i18n - return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values) - } - } - }) - // $FlowFixMe - Object.defineProperty(Vue.prototype, '$tc', { - get () { - return (key: Path, choice?: number, ...values: any): TranslateResult => { - const i18n = this.$i18n - return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values) - } - } - }) - // $FlowFixMe - Object.defineProperty(Vue.prototype, '$te', { - get () { - return (key: Path, locale?: Locale): boolean => { - const i18n = this.$i18n - return i18n._te(key, i18n.locale, i18n._getMessages(), locale) - } - } - }) - // $FlowFixMe - Object.defineProperty(Vue.prototype, '$d', { - get () { - return (value: number | Date, ...args: any): DateTimeFormatResult => { - return this.$i18n.d(value, ...args) - } - } - }) - // $FlowFixMe - Object.defineProperty(Vue.prototype, '$n', { - get () { - return (value: number, ...args: any): NumberFormatResult => { - return this.$i18n.n(value, ...args) - } - } + Object.defineProperty(Vue.prototype, '$i18n', { + get () { return this._i18n } }) + + Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult { + const i18n = this.$i18n + return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values) + } + + Vue.prototype.$tc = function (key: Path, choice?: number, ...values: any): TranslateResult { + const i18n = this.$i18n + return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values) + } + + Vue.prototype.$te = function (key: Path, locale?: Locale): boolean { + const i18n = this.$i18n + return i18n._te(key, i18n.locale, i18n._getMessages(), locale) + } + + Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult { + return this.$i18n.d(value, ...args) + } + + Vue.prototype.$n = function (value: number, ...args: any): NumberFormatResult { + return this.$i18n.n(value, ...args) + } } diff --git a/src/install.js b/src/install.js index 750206eeb..8066afd57 100644 --- a/src/install.js +++ b/src/install.js @@ -23,10 +23,6 @@ export function install (_Vue) { return } - Object.defineProperty(Vue.prototype, '$i18n', { - get () { return this._i18n } - }) - extend(Vue) Vue.mixin(mixin) Vue.directive('t', { bind, update, unbind }) diff --git a/test/unit/issues.test.js b/test/unit/issues.test.js index 782d391a1..bd1b76699 100644 --- a/test/unit/issues.test.js +++ b/test/unit/issues.test.js @@ -287,35 +287,6 @@ describe('issues', () => { }) }) - describe('#259', () => { - it('this points to the right', done => { - const vm = new Vue({ - i18n: new VueI18n({ - locale: 'en', - messages: { - en: { - 'hello': 'hello #259' - }, - ja: { - 'hello': 'こんにちは #259' - } - } - }) - }) - const $t = vm.$t - const $tc = vm.$t - const $te = vm.$t - const $d = vm.$t - const $n = vm.$t - assert.equal($t('hello'), 'hello #259') - assert.equal($tc('hello'), 'hello #259') - assert.equal($te('hello'), 'hello #259') - assert.equal($d('hello'), 'hello #259') - assert.equal($n('hello'), 'hello #259') - done() - }) - }) - describe('#377', () => { it('should be destroyed', done => { const el = document.createElement('div')