|
|
@@ -1,31 +1,37 @@
|
|
|
var _ = require('../util')
|
|
|
+var Watcher = require('../watcher')
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
priority: 900,
|
|
|
|
|
|
bind: function () {
|
|
|
- if (this.el !== this.vm.$el) {
|
|
|
- this.invalid = true
|
|
|
+ var vm = this.vm
|
|
|
+ if (this.el !== vm.$el) {
|
|
|
_.warn(
|
|
|
'v-with can only be used on instance root elements.'
|
|
|
)
|
|
|
- return
|
|
|
- }
|
|
|
- if (this.arg) {
|
|
|
- var self = this
|
|
|
- this.vm.$watch(this.arg, function (val) {
|
|
|
- self.set(_.toNumber(val))
|
|
|
- })
|
|
|
+ } else if (!vm.$parent) {
|
|
|
+ _.warn(
|
|
|
+ 'v-with must be used on an instance with a parent.'
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ var key = this.arg
|
|
|
+ this.watcher = new Watcher(
|
|
|
+ vm.$parent,
|
|
|
+ this.expression,
|
|
|
+ function (val) {
|
|
|
+ vm.$set(key, val)
|
|
|
+ }
|
|
|
+ )
|
|
|
+ // initial set
|
|
|
+ vm.$set(key, this.watcher.value)
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- update: function (value) {
|
|
|
- if (this.invalid) return
|
|
|
- if (this.arg) {
|
|
|
- this.vm.$set(this.arg, value)
|
|
|
- } else if (this.vm.$data !== value) {
|
|
|
- this.vm.$data = value
|
|
|
+ unbind: function () {
|
|
|
+ if (this.watcher) {
|
|
|
+ this.watcher.teardown()
|
|
|
}
|
|
|
}
|
|
|
|