From b379274b31c8f94e45e5d407517745895fe1f998 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 25 Aug 2013 14:16:13 -0400 Subject: [PATCH] make it cleaner --- examples/todomvc/index.html | 4 ++-- examples/todomvc/js/app.js | 11 ++++------- src/compiler.js | 4 +++- src/directives/index.js | 2 +- src/directives/on.js | 2 +- src/exp-parser.js | 18 ++++++++++++++---- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/todomvc/index.html b/examples/todomvc/index.html index 5fd26eef4..92a7fc00c 100644 --- a/examples/todomvc/index.html +++ b/examples/todomvc/index.html @@ -29,7 +29,7 @@ class="todo" sd-each="todo:todos" sd-show="todoFilter(todo)" - sd-class="completed:todo.completed, editing:todo.editing" + sd-class="completed:todo.completed, editing:editedTodo===todo" >
diff --git a/examples/todomvc/js/app.js b/examples/todomvc/js/app.js index ab443667a..9d98e69ca 100644 --- a/examples/todomvc/js/app.js +++ b/examples/todomvc/js/app.js @@ -1,7 +1,4 @@ -seed.config({ debug: false }) - var filters = { - // need to access todo.completed in here so Seed.js can capture dependency. all: function (todo) { return todo.completed || true }, active: function (todo) { return !todo.completed }, completed: function (todo) { return todo.completed } @@ -46,19 +43,19 @@ var Todos = seed.ViewModel.extend({ editTodo: function (e) { this.beforeEditCache = e.item.title - e.item.editing = true + this.editedTodo = e.item }, doneEdit: function (e) { - if (!e.item.editing) return - e.item.editing = false + if (!this.editedTodo) return + this.editedTodo = null e.item.title = e.item.title.trim() if (!e.item.title) this.removeTodo(e) todoStorage.save() }, cancelEdit: function (e) { - e.item.editing = false + this.editedTodo = null e.item.title = this.beforeEditCache }, diff --git a/src/compiler.js b/src/compiler.js index 056a380b0..ab2ea310d 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -391,7 +391,9 @@ CompilerProto.define = function (key, binding) { enumerable: true, get: function () { var value = binding.value - if ((!binding.isComputed && (value === undefined || !value.__observer__)) || Array.isArray(value)) { + if ((!binding.isComputed && + (value === undefined || value === null || !value.__observer__)) || + Array.isArray(value)) { // only emit non-computed, non-observed (tip) values, or Arrays. // because these are the cleanest dependencies compiler.observer.emit('get', key) diff --git a/src/directives/index.js b/src/directives/index.js index cebfe1394..9fe9b7f5a 100644 --- a/src/directives/index.js +++ b/src/directives/index.js @@ -30,7 +30,7 @@ module.exports = { focus: function (value) { var el = this.el setTimeout(function () { - el[value ? 'focus' : 'focus']() + if (value) el.focus() }, 0) }, diff --git a/src/directives/on.js b/src/directives/on.js index 87325c06e..0fb468364 100644 --- a/src/directives/on.js +++ b/src/directives/on.js @@ -64,7 +64,7 @@ module.exports = { if (compiler.each) { e.item = vm[compiler.eachPrefix] } - handler.call(vm, e) + handler.call(ownerVM, e) } this.el.addEventListener(event, this.handler) diff --git a/src/exp-parser.js b/src/exp-parser.js index 4efa6d560..adfeb63d2 100644 --- a/src/exp-parser.js +++ b/src/exp-parser.js @@ -1,6 +1,4 @@ -/* - * Variable extraction scooped from https://github.com/RubyLouvre/avalon - */ +// Variable extraction scooped from https://github.com/RubyLouvre/avalon var KEYWORDS = // keywords 'break,case,catch,continue,debugger,default,delete,do,else,false' @@ -32,7 +30,13 @@ function getVariables (code) { } module.exports = { - parseGetter: function (exp) { + + /* + * Parse and create an anonymous computed property getter function + * from an arbitrary expression. + */ + parseGetter: function (exp, compiler) { + // extract variable names var vars = getVariables(exp) if (!vars.length) return null var args = [], @@ -40,9 +44,15 @@ module.exports = { hash = {} while (i--) { v = vars[i] + // avoid duplicate keys if (hash[v]) continue hash[v] = 1 + // push assignment args.push(v + '=this.$get("' + v + '")') + // need to create the binding if it does not exist yet + if (!compiler.bindings[v]) { + compiler.rootCompiler.createBinding(v) + } } args = 'var ' + args.join(',') + ';return ' + exp /* jshint evil: true */