Skip to content

Commit

Permalink
fix dependency tracking for nested values
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan You committed Oct 21, 2013
1 parent fb6b4ea commit db22a2a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/compiler.js
Expand Up @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion src/exp-parser.js
Expand Up @@ -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 = {

/**
Expand Down Expand Up @@ -61,7 +66,7 @@ module.exports = {
/* jshint evil: true */
return {
getter: new Function(args),
vars: Object.keys(hash)
paths: getPaths(exp, Object.keys(hash))
}
}
}
7 changes: 4 additions & 3 deletions test/unit/specs/exp-parser.js
Expand Up @@ -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',
Expand All @@ -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])
}
})

Expand Down

0 comments on commit db22a2a

Please sign in to comment.