Skip to content

Commit

Permalink
new observe method tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 23, 2013
1 parent 29b91b5 commit 93b0325
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/todomvc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h1>todos</h1>
</section>
<footer id="info">
<p>Double-click to edit a todo</p>
<p>Powered by <a href="https://github.com/yyx990803/vue">Vue.js</a></p>
<p>Powered by <a href="https://github.com/yyx990803/vue">VueJS</a></p>
<p>Created by <a href="http://evanyou.me">Evan You</a></p>
</footer>

Expand Down
2 changes: 0 additions & 2 deletions examples/todomvc/js/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Vue.config({debug:true})

var filters = {
all: function () { return true },
active: function (completed) { return !completed },
Expand Down
31 changes: 17 additions & 14 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function Compiler (vm, options) {
compiler.rootCompiler = parent
? getRoot(parent)
: compiler
def(vm, '$root', compiler.rootCompiler.vm)

// set parent VM
// and register child id on parent
Expand Down Expand Up @@ -187,7 +188,7 @@ CompilerProto.setupObserver = function () {
})

function check (key) {
if (!bindings[key]) {
if (!hasOwn.call(bindings, key)) {
compiler.createBinding(key)
}
}
Expand Down Expand Up @@ -471,20 +472,22 @@ CompilerProto.define = function (key, binding) {
}

Object.defineProperty(vm, key, {
get: function () {
var val = scope[key]
return binding.isComputed
? val.$get()
: val
},
set: function (newVal) {
var val = scope[key]
if (binding.isComputed) {
if (val.$set) val.$set(newVal)
} else {
scope[key] = newVal
get: binding.isComputed
? function () {
return scope[key].$get()
}
: function () {
return scope[key]
},
set: binding.isComputed
? function (val) {
if (scope[key].$set) {
scope[key].$set(val)
}
}
: function (val) {
scope[key] = val
}
}
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var config = require('./config'),
ARG_RE = /^([\w- ]+):(.+)$/,
FILTERS_RE = /\|[^\|]+/g,
FILTER_TOKEN_RE = /[^\s']+|'[^']+'/g,
NESTING_RE = /^\$(parent|root)\./,
SINGLE_VAR_RE = /^[\w\.\$]+$/

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ function Directive (definition, expression, rawKey, compiler, node) {

parseKey(this, rawKey)

this.isExp = !SINGLE_VAR_RE.test(this.key)
this.isExp = !SINGLE_VAR_RE.test(this.key) || NESTING_RE.test(this.key)

var filterExps = this.expression.slice(rawKey.length).match(FILTERS_RE)
if (filterExps) {
Expand Down
9 changes: 5 additions & 4 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ for (var method in extensions) {
*/
function watchObject (obj) {
for (var key in obj) {
var keyPrefix = key.charAt(0)
if ((keyPrefix !== '$' && keyPrefix !== '_') || key === '$index') {
convert(obj, key)
}
convert(obj, key)
}
}

Expand Down Expand Up @@ -128,6 +125,10 @@ function watchArray (arr, path) {
* Then watch the value itself.
*/
function convert (obj, key) {
var keyPrefix = key.charAt(0)
if ((keyPrefix === '$' || keyPrefix === '_') && key !== '$index') {
return
}
var observer = obj.__observer__,
val = obj[key],
values = observer.values
Expand Down
8 changes: 4 additions & 4 deletions test/functional/fixtures/nested-viewmodels.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<p class="ancestor">{{name}} {{family}}</p>

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

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

<div v-component="offspring" data-name="Tim" class="tim">
</div>
Expand All @@ -37,7 +37,7 @@
</div>

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

<div v-component="offspring" data-name="Andrew" class="andrew">
</div>
Expand All @@ -46,7 +46,7 @@
</div>

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

<script>
Expand Down

0 comments on commit 93b0325

Please sign in to comment.