From db22a2a0239d3d1e9eb39f1453f3770d04384df8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 21 Oct 2013 14:17:20 -0400 Subject: [PATCH] fix dependency tracking for nested values --- src/compiler.js | 4 ++-- src/exp-parser.js | 7 ++++++- test/unit/specs/exp-parser.js | 7 ++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/compiler.js b/src/compiler.js index 2e641f3f9f6..cd9e39e111f 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -396,9 +396,9 @@ CompilerProto.createBinding = function (key, isExp) { compiler.exps.push(binding) // need to create the bindings for keys // that do not exist yet - var i = result.vars.length, v + var i = result.paths.length, v while (i--) { - v = result.vars[i] + v = result.paths[i] if (!bindings[v]) { compiler.rootCompiler.createBinding(v) } diff --git a/src/exp-parser.js b/src/exp-parser.js index 63f1a322e46..98cc45edd2b 100644 --- a/src/exp-parser.js +++ b/src/exp-parser.js @@ -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 = { /** @@ -61,7 +66,7 @@ module.exports = { /* jshint evil: true */ return { getter: new Function(args), - vars: Object.keys(hash) + paths: getPaths(exp, Object.keys(hash)) } } } \ No newline at end of file diff --git a/test/unit/specs/exp-parser.js b/test/unit/specs/exp-parser.js index 7ea00049999..c490571201a 100644 --- a/test/unit/specs/exp-parser.js +++ b/test/unit/specs/exp-parser.js @@ -44,6 +44,7 @@ describe('UNIT: Expression Parser', function () { { // complex with nested values exp: "todo.title + ' : ' + (todo.done ? 'yep' : 'nope')", + paths: ['todo.title', 'todo.done'], vm: { todo: { title: 'write tests', @@ -61,16 +62,16 @@ describe('UNIT: Expression Parser', function () { var result = ExpParser.parse(testCase.exp), vm = testCase.vm, - vars = Object.keys(vm) + vars = testCase.paths || Object.keys(vm) // mock the $get(). // the real $get() will be tested in integration tests. vm.$get = function (key) { return this[key] } 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++) { - assert.strictEqual(vars[i], result.vars[i]) + assert.strictEqual(vars[i], result.paths[i]) } })