Skip to content

Commit 93b0325

Browse files
committed
new observe method tests passed
1 parent 29b91b5 commit 93b0325

File tree

6 files changed

+29
-26
lines changed

6 files changed

+29
-26
lines changed

examples/todomvc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ <h1>todos</h1>
7474
</section>
7575
<footer id="info">
7676
<p>Double-click to edit a todo</p>
77-
<p>Powered by <a href="https://github.com/yyx990803/vue">Vue.js</a></p>
77+
<p>Powered by <a href="https://github.com/yyx990803/vue">VueJS</a></p>
7878
<p>Created by <a href="http://evanyou.me">Evan You</a></p>
7979
</footer>
8080

examples/todomvc/js/app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Vue.config({debug:true})
2-
31
var filters = {
42
all: function () { return true },
53
active: function (completed) { return !completed },

src/compiler.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function Compiler (vm, options) {
6767
compiler.rootCompiler = parent
6868
? getRoot(parent)
6969
: compiler
70+
def(vm, '$root', compiler.rootCompiler.vm)
7071

7172
// set parent VM
7273
// and register child id on parent
@@ -187,7 +188,7 @@ CompilerProto.setupObserver = function () {
187188
})
188189

189190
function check (key) {
190-
if (!bindings[key]) {
191+
if (!hasOwn.call(bindings, key)) {
191192
compiler.createBinding(key)
192193
}
193194
}
@@ -471,20 +472,22 @@ CompilerProto.define = function (key, binding) {
471472
}
472473

473474
Object.defineProperty(vm, key, {
474-
get: function () {
475-
var val = scope[key]
476-
return binding.isComputed
477-
? val.$get()
478-
: val
479-
},
480-
set: function (newVal) {
481-
var val = scope[key]
482-
if (binding.isComputed) {
483-
if (val.$set) val.$set(newVal)
484-
} else {
485-
scope[key] = newVal
475+
get: binding.isComputed
476+
? function () {
477+
return scope[key].$get()
478+
}
479+
: function () {
480+
return scope[key]
481+
},
482+
set: binding.isComputed
483+
? function (val) {
484+
if (scope[key].$set) {
485+
scope[key].$set(val)
486+
}
487+
}
488+
: function (val) {
489+
scope[key] = val
486490
}
487-
}
488491
})
489492
}
490493

src/directive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var config = require('./config'),
1515
ARG_RE = /^([\w- ]+):(.+)$/,
1616
FILTERS_RE = /\|[^\|]+/g,
1717
FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
18+
NESTING_RE = /^\$(parent|root)\./,
1819
SINGLE_VAR_RE = /^[\w\.\$]+$/
1920

2021
/**
@@ -53,7 +54,7 @@ function Directive (definition, expression, rawKey, compiler, node) {
5354

5455
parseKey(this, rawKey)
5556

56-
this.isExp = !SINGLE_VAR_RE.test(this.key)
57+
this.isExp = !SINGLE_VAR_RE.test(this.key) || NESTING_RE.test(this.key)
5758

5859
var filterExps = this.expression.slice(rawKey.length).match(FILTERS_RE)
5960
if (filterExps) {

src/observer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ for (var method in extensions) {
9595
*/
9696
function watchObject (obj) {
9797
for (var key in obj) {
98-
var keyPrefix = key.charAt(0)
99-
if ((keyPrefix !== '$' && keyPrefix !== '_') || key === '$index') {
100-
convert(obj, key)
101-
}
98+
convert(obj, key)
10299
}
103100
}
104101

@@ -128,6 +125,10 @@ function watchArray (arr, path) {
128125
* Then watch the value itself.
129126
*/
130127
function convert (obj, key) {
128+
var keyPrefix = key.charAt(0)
129+
if ((keyPrefix === '$' || keyPrefix === '_') && key !== '$index') {
130+
return
131+
}
131132
var observer = obj.__observer__,
132133
val = obj[key],
133134
values = observer.values

test/functional/fixtures/nested-viewmodels.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<p class="ancestor">{{name}} {{family}}</p>
2525

2626
<div v-component="man" data-name="Jack">
27-
<p class="jack">{{name}}, son of {{^name}}</p>
27+
<p class="jack">{{name}}, son of {{$parent.name}}</p>
2828

2929
<div v-component="man" data-name="Mike">
30-
<p class="mike">{{name}}, son of {{^name}}</p>
30+
<p class="mike">{{name}}, son of {{$parent.name}}</p>
3131

3232
<div v-component="offspring" data-name="Tim" class="tim">
3333
</div>
@@ -37,7 +37,7 @@
3737
</div>
3838

3939
<div v-component="man" data-name="Jason">
40-
<p class="jason">{{name}}, son of {{^name}}</p>
40+
<p class="jason">{{name}}, son of {{$parent.name}}</p>
4141

4242
<div v-component="offspring" data-name="Andrew" class="andrew">
4343
</div>
@@ -46,7 +46,7 @@
4646
</div>
4747

4848
<script type="text/v-template" id="v-template-offspring">
49-
<p>{{name}}, son of {{^name}}, grandson of {{^^name}}, great-grandson of {{*name}}, and offspring of family {{family}}.</p>
49+
<p>{{name}}, son of {{$parent.name}}, grandson of {{$parent.$parent.name}}, great-grandson of {{$root.name}}, and offspring of family {{family}}.</p>
5050
</script>
5151

5252
<script>

0 commit comments

Comments
 (0)