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)
// 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)
}

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 = {
/**
@ -61,7 +66,7 @@ module.exports = {
/* jshint evil: true */
return {
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
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])
}
})