fix dependency tracking for nested values

This commit is contained in:
Evan You 2013-10-21 14:17:20 -04:00
parent fb6b4eaca5
commit db22a2a023
3 changed files with 12 additions and 6 deletions

View File

@ -396,9 +396,9 @@ CompilerProto.createBinding = function (key, isExp) {
compiler.exps.push(binding) compiler.exps.push(binding)
// need to create the bindings for keys // need to create the bindings for keys
// that do not exist yet // that do not exist yet
var i = result.vars.length, v var i = result.paths.length, v
while (i--) { while (i--) {
v = result.vars[i] v = result.paths[i]
if (!bindings[v]) { if (!bindings[v]) {
compiler.rootCompiler.createBinding(v) compiler.rootCompiler.createBinding(v)
} }

View File

@ -30,6 +30,11 @@ function getVariables (code) {
: [] : []
} }
function getPaths (code, vars) {
var pathRE = new RegExp("\\b(" + vars.join('|') + ")[$\\w\\.]*\\b", 'g')
return code.match(pathRE)
}
module.exports = { module.exports = {
/** /**
@ -61,7 +66,7 @@ module.exports = {
/* jshint evil: true */ /* jshint evil: true */
return { return {
getter: new Function(args), getter: new Function(args),
vars: Object.keys(hash) paths: getPaths(exp, Object.keys(hash))
} }
} }
} }

View File

@ -44,6 +44,7 @@ describe('UNIT: Expression Parser', function () {
{ {
// complex with nested values // complex with nested values
exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')", exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')",
paths: ['todo.title', 'todo.done'],
vm: { vm: {
todo: { todo: {
title: 'write tests', title: 'write tests',
@ -61,16 +62,16 @@ describe('UNIT: Expression Parser', function () {
var result = ExpParser.parse(testCase.exp), var result = ExpParser.parse(testCase.exp),
vm = testCase.vm, vm = testCase.vm,
vars = Object.keys(vm) vars = testCase.paths || Object.keys(vm)
// mock the $get(). // mock the $get().
// the real $get() will be tested in integration tests. // the real $get() will be tested in integration tests.
vm.$get = function (key) { return this[key] } vm.$get = function (key) { return this[key] }
it('should get correct args', function () { it('should get correct args', function () {
assert.strictEqual(result.vars.length, vars.length) assert.strictEqual(result.paths.length, vars.length)
for (var i = 0; i < vars.length; i++) { for (var i = 0; i < vars.length; i++) {
assert.strictEqual(vars[i], result.vars[i]) assert.strictEqual(vars[i], result.paths[i])
} }
}) })