@@ -3,88 +3,136 @@ var utils = require('../utils')
33module . exports = {
44
55 bind : function ( ) {
6- if ( this . el . vue_vm ) {
7- this . subVM = this . el . vue_vm
8- var compiler = this . subVM . $compiler
9- if ( this . arg && ! compiler . bindings [ this . arg ] ) {
10- compiler . createBinding ( this . arg )
11- }
12- } else if ( this . isEmpty ) {
13- this . build ( )
14- }
15- } ,
166
17- update : function ( value , init ) {
18- var vm = this . subVM ,
19- key = this . arg || '$data'
20- if ( ! vm ) {
21- this . build ( value )
22- } else if ( ! this . lock && vm [ key ] !== value ) {
23- vm [ key ] = value
24- }
25- if ( init ) {
26- // watch after first set
27- this . watch ( )
28- // The v-with directive can have multiple expressions,
29- // and we want to make sure when the ready hook is called
30- // on the subVM, all these clauses have been properly set up.
31- // So this is a hack that sniffs whether we have reached
32- // the last expression. We hold off the subVM's ready hook
33- // until we are actually ready.
34- if ( this . last ) {
35- this . subVM . $compiler . execHook ( 'ready' )
36- }
37- }
38- } ,
7+ var self = this ,
8+ childKey = self . arg ,
9+ parentKey = self . key ,
10+ compiler = self . compiler ,
11+ owner = self . binding . compiler
3912
40- build : function ( value ) {
41- var data = value
42- if ( this . arg ) {
43- data = { }
44- data [ this . arg ] = value
13+ if ( compiler === owner ) {
14+ this . alone = true
15+ return
4516 }
46- var Ctor = this . compiler . resolveComponent ( this . el , data )
47- this . subVM = new Ctor ( {
48- el : this . el ,
49- data : data ,
50- parent : this . vm ,
51- compilerOptions : {
52- // it is important to delay the ready hook
53- // so that when it's called, all `v-with` wathcers
54- // would have been set up.
55- delayReady : ! this . last
56- }
57- } )
58- // mark that this VM is created by v-with
59- utils . defProtected ( this . subVM , '$with' , true )
60- } ,
6117
62- /**
63- * For inhertied keys, need to watch
64- * and sync back to the parent
65- */
66- watch : function ( ) {
67- if ( ! this . arg ) return
68- var self = this ,
69- key = self . key ,
70- ownerVM = self . binding . compiler . vm
71- this . subVM . $compiler . observer . on ( 'change:' + this . arg , function ( val ) {
72- if ( ! self . lock ) {
73- self . lock = true
74- utils . nextTick ( function ( ) {
75- self . lock = false
76- } )
18+ if ( childKey ) {
19+ if ( ! compiler . bindings [ childKey ] ) {
20+ compiler . createBinding ( childKey )
7721 }
78- ownerVM . $set ( key , val )
79- } )
22+ // sync changes on child back to parent
23+ compiler . observer . on ( 'change:' + childKey , function ( val ) {
24+ if ( compiler . init ) return
25+ if ( ! self . lock ) {
26+ self . lock = true
27+ utils . nextTick ( function ( ) {
28+ self . lock = false
29+ } )
30+ }
31+ owner . vm . $set ( parentKey , val )
32+ } )
33+ }
8034 } ,
8135
82- unbind : function ( ) {
83- // all watchers are turned off during destroy
84- // so no need to worry about it
85- if ( this . subVM . $with ) {
86- this . subVM . $destroy ( )
36+ update : function ( value ) {
37+ // sync from parent
38+ if ( ! this . alone && ! this . lock ) {
39+ if ( this . arg ) {
40+ this . vm . $set ( this . arg , value )
41+ } else {
42+ this . vm . $data = value
43+ }
8744 }
8845 }
8946
90- }
47+ }
48+
49+ // var utils = require('../utils')
50+
51+ // module.exports = {
52+
53+ // bind: function () {
54+ // if (this.el.vue_vm) {
55+ // this.subVM = this.el.vue_vm
56+ // var compiler = this.subVM.$compiler
57+ // if (this.arg && !compiler.bindings[this.arg]) {
58+ // compiler.createBinding(this.arg)
59+ // }
60+ // } else if (this.isEmpty) {
61+ // this.build()
62+ // }
63+ // },
64+
65+ // update: function (value, init) {
66+ // var vm = this.subVM,
67+ // key = this.arg || '$data'
68+ // if (!vm) {
69+ // this.build(value)
70+ // } else if (!this.lock && vm[key] !== value) {
71+ // vm[key] = value
72+ // }
73+ // if (init) {
74+ // // watch after first set
75+ // this.watch()
76+ // // The v-with directive can have multiple expressions,
77+ // // and we want to make sure when the ready hook is called
78+ // // on the subVM, all these clauses have been properly set up.
79+ // // So this is a hack that sniffs whether we have reached
80+ // // the last expression. We hold off the subVM's ready hook
81+ // // until we are actually ready.
82+ // if (this.last) {
83+ // this.subVM.$compiler.execHook('ready')
84+ // }
85+ // }
86+ // },
87+
88+ // build: function (value) {
89+ // var data = value
90+ // if (this.arg) {
91+ // data = {}
92+ // data[this.arg] = value
93+ // }
94+ // var Ctor = this.compiler.resolveComponent(this.el, data)
95+ // this.subVM = new Ctor({
96+ // el : this.el,
97+ // data : data,
98+ // parent : this.vm,
99+ // compilerOptions: {
100+ // // it is important to delay the ready hook
101+ // // so that when it's called, all `v-with` wathcers
102+ // // would have been set up.
103+ // delayReady: !this.last
104+ // }
105+ // })
106+ // // mark that this VM is created by v-with
107+ // utils.defProtected(this.subVM, '$with', true)
108+ // },
109+
110+ // /**
111+ // * For inhertied keys, need to watch
112+ // * and sync back to the parent
113+ // */
114+ // watch: function () {
115+ // if (!this.arg) return
116+ // var self = this,
117+ // key = self.key,
118+ // ownerVM = self.binding.compiler.vm
119+ // this.subVM.$compiler.observer.on('change:' + this.arg, function (val) {
120+ // if (!self.lock) {
121+ // self.lock = true
122+ // utils.nextTick(function () {
123+ // self.lock = false
124+ // })
125+ // }
126+ // ownerVM.$set(key, val)
127+ // })
128+ // },
129+
130+ // unbind: function () {
131+ // // all watchers are turned off during destroy
132+ // // so no need to worry about it
133+ // if (this.subVM.$with) {
134+ // this.subVM.$destroy()
135+ // }
136+ // }
137+
138+ // }
0 commit comments