Skip to content

Commit

Permalink
bindings that are not found are now created on current compiler inste…
Browse files Browse the repository at this point in the history
…ad of root
  • Loading branch information
Evan You committed Jan 21, 2014
1 parent 09b44b8 commit 1fb885b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
16 changes: 9 additions & 7 deletions examples/todomvc/js/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

var app = new Vue({

// the root element that will be compiled
Expand All @@ -14,9 +16,10 @@ var app = new Vue({
}
},

// the `created` lifecycle hook.
// it will be called when the ViewModel instance is created.
// the `created` lifecycle hook, which will be called
// when the ViewModel instance is created but not yet compiled.
created: function () {
// setup filters
this.filters = {
all: function (todo) { todo.completed; return true },
active: function (todo) { return !todo.completed },
Expand All @@ -26,17 +29,16 @@ var app = new Vue({
window.addEventListener('hashchange', function () {
app.updateFilter()
})
this.remaining = this.todos.filter(function (todo) {
return !todo.completed
}).length
// initialize some state
this.newTodo = ''
this.editedTodo = null
this.remaining = this.todos.filter(this.filters.active).length
},

// data
data: {

// fetch the saved todos from localStorage
todos: todoStorage.fetch(),

// a computed property with custom getter/setter
allDone: {
$get: function () {
Expand Down
30 changes: 17 additions & 13 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,20 +412,24 @@ CompilerProto.bindDirective = function (directive) {
if (directive.isExp) {
// expression bindings are always created on current compiler
binding = compiler.createBinding(key, true, directive.isFn)
} else if (
hasOwn.call(compiler.data, baseKey) ||
hasOwn.call(compiler.vm, baseKey)
) {
// If the directive's compiler's VM has the base key,
// it belongs here. Create the binding if it's not created already.
binding = hasOwn.call(compiler.bindings, key)
? compiler.bindings[key]
: compiler.createBinding(key)
} else {
// due to prototypal inheritance of bindings, if a key doesn't exist
// on the bindings object, then it doesn't exist in the whole
// prototype chain. In this case we create the new binding at the root level.
binding = compiler.bindings[key] || compiler.rootCompiler.createBinding(key)
// recursively locate where to place the binding
while (compiler) {
if (
hasOwn.call(compiler.data, baseKey) ||
hasOwn.call(compiler.vm, baseKey)
) {
// If a compiler has the base key, the directive should
// belong to it. Create the binding if it's not created already.
binding = hasOwn.call(compiler.bindings, key)
? compiler.bindings[key]
: compiler.createBinding(key)
break
} else {
compiler = compiler.parentCompiler
}
}
if (!binding) binding = this.createBinding(key)
}

binding.instances.push(directive)
Expand Down
26 changes: 16 additions & 10 deletions src/exp-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,38 @@ function getVariables (code) {
* final resolved vm.
*/
function getRel (path, compiler) {
var rel = '',
vm = compiler.vm,
dot = path.indexOf('.'),
key = dot > -1
var rel = '',
has = false,
nest = 0,
vm = compiler.vm,
dot = path.indexOf('.'),
key = dot > -1
? path.slice(0, dot)
: path
while (true) {
if (
hasOwn.call(vm.$data, key) ||
hasOwn.call(vm, key)
) {
has = true
break
} else {
if (vm.$parent) {
vm = vm.$parent
rel += '$parent.'
nest++
} else {
break
}
}
}
compiler = vm.$compiler
if (
!hasOwn.call(compiler.bindings, path) &&
path.charAt(0) !== '$'
) {
if (has) {
while (nest--) {
rel += '$parent.'
}
if (!hasOwn.call(vm.$compiler.bindings, path) && path.charAt(0) !== '$') {
vm.$compiler.createBinding(path)
}
} else {
compiler.createBinding(path)
}
return rel
Expand Down
10 changes: 7 additions & 3 deletions test/unit/specs/exp-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ describe('UNIT: Expression Parser', function () {
function describeCase (testCase) {
describe(testCase.exp, function () {

function createBinding (path) {
caughtMissingPaths.push(path)
}

var caughtMissingPaths = [],
compilerMock = {
createBinding: createBinding,
vm:{
$data: {},
$compiler:{
bindings:{},
createBinding: function (path) {
caughtMissingPaths.push(path)
}
createBinding: createBinding
}
}
},
Expand Down Expand Up @@ -118,6 +121,7 @@ describe('UNIT: Expression Parser', function () {
warned = true
}
ExpParser.parse('a + "fsef', {
createBinding: function () {},
vm: {
$compiler: {
bindings: {},
Expand Down

0 comments on commit 1fb885b

Please sign in to comment.