This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lang): support lang reactive changing
- Loading branch information
Showing
6 changed files
with
135 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var webpack = require('webpack') | ||
|
||
module.exports = { | ||
entry: './index.js', | ||
output: { | ||
path: './', | ||
publicPath: '/', | ||
filename: 'build.js' | ||
}, | ||
devtool: 'source-map', | ||
module: { | ||
preLoaders: [{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
loader: 'eslint-loader' | ||
}], | ||
loaders: [{ | ||
test: /\.js$/, | ||
exclude: /node_modules|vue\/dist/, | ||
loader: 'babel', | ||
query: { | ||
presets: ['es2015'] | ||
} | ||
}] | ||
}, | ||
devServer: { | ||
contentBase: './', | ||
port: 8080, | ||
hot: true, | ||
inline: true | ||
}, | ||
plugins: [ | ||
new webpack.HotModuleReplacementPlugin() | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getWatcher, getDep } from './util' | ||
|
||
export default function (Vue, langVM) { | ||
const Watcher = getWatcher(langVM) | ||
const Dep = getDep(langVM) | ||
|
||
function makeComputedGetter (getter, owner) { | ||
let watcher = new Watcher(owner, getter, null, { | ||
lazy: true | ||
}) | ||
|
||
return function computedGetter () { | ||
if (watcher.dirty) { | ||
watcher.evaluate() | ||
} | ||
if (Dep.target) { | ||
watcher.depend() | ||
} | ||
return watcher.value | ||
} | ||
} | ||
|
||
// define Vue.config.lang configration | ||
Object.defineProperty(Vue.config, 'lang', { | ||
enumerable: true, | ||
configurable: true, | ||
get: makeComputedGetter(() => { return langVM.lang }, langVM), | ||
set: Vue.util.bind((val) => { langVM.lang = val }, langVM) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export default function (Vue, langVM) { | ||
// override _init | ||
const init = Vue.prototype._init | ||
Vue.prototype._init = function (options) { | ||
options = options || {} | ||
let root = options._parent || options.parent || this | ||
let lang = root.$lang | ||
|
||
if (lang) { | ||
this.$lang = lang | ||
} else { | ||
this.$lang = langVM | ||
} | ||
|
||
this._langUnwatch = this.$lang.$watch('lang', (a, b) => { | ||
update(this) | ||
}) | ||
|
||
init.call(this, options) | ||
} | ||
|
||
// override _destroy | ||
const destroy = Vue.prototype._destroy | ||
Vue.prototype._destroy = function () { | ||
if (this._langUnwatch) { | ||
this._langUnwatch() | ||
this._langUnwatch = null | ||
} | ||
|
||
this.$lang = null | ||
destroy.apply(this, arguments) | ||
} | ||
} | ||
|
||
function update (vm) { | ||
let i = vm._watchers.length | ||
while (i--) { | ||
vm._watchers[i].update(true) // shallow updates | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters