make it cleaner

This commit is contained in:
Evan You 2013-08-25 14:16:13 -04:00
parent d4beb35b68
commit b379274b31
6 changed files with 25 additions and 16 deletions

View File

@ -29,7 +29,7 @@
class="todo" class="todo"
sd-each="todo:todos" sd-each="todo:todos"
sd-show="todoFilter(todo)" sd-show="todoFilter(todo)"
sd-class="completed:todo.completed, editing:todo.editing" sd-class="completed:todo.completed, editing:editedTodo===todo"
> >
<div class="view"> <div class="view">
<input <input
@ -44,7 +44,7 @@
<input <input
class="edit" class="edit"
type="text" type="text"
sd-focus="todo.editing" sd-focus="editedTodo===todo"
sd-on="blur:doneEdit, keyup:doneEdit | key enter, keyup:cancelEdit | key esc" sd-on="blur:doneEdit, keyup:doneEdit | key enter, keyup:cancelEdit | key esc"
sd-value="todo.title" sd-value="todo.title"
> >

View File

@ -1,7 +1,4 @@
seed.config({ debug: false })
var filters = { var filters = {
// need to access todo.completed in here so Seed.js can capture dependency.
all: function (todo) { return todo.completed || true }, all: function (todo) { return todo.completed || true },
active: function (todo) { return !todo.completed }, active: function (todo) { return !todo.completed },
completed: function (todo) { return todo.completed } completed: function (todo) { return todo.completed }
@ -46,19 +43,19 @@ var Todos = seed.ViewModel.extend({
editTodo: function (e) { editTodo: function (e) {
this.beforeEditCache = e.item.title this.beforeEditCache = e.item.title
e.item.editing = true this.editedTodo = e.item
}, },
doneEdit: function (e) { doneEdit: function (e) {
if (!e.item.editing) return if (!this.editedTodo) return
e.item.editing = false this.editedTodo = null
e.item.title = e.item.title.trim() e.item.title = e.item.title.trim()
if (!e.item.title) this.removeTodo(e) if (!e.item.title) this.removeTodo(e)
todoStorage.save() todoStorage.save()
}, },
cancelEdit: function (e) { cancelEdit: function (e) {
e.item.editing = false this.editedTodo = null
e.item.title = this.beforeEditCache e.item.title = this.beforeEditCache
}, },

View File

@ -391,7 +391,9 @@ CompilerProto.define = function (key, binding) {
enumerable: true, enumerable: true,
get: function () { get: function () {
var value = binding.value 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. // only emit non-computed, non-observed (tip) values, or Arrays.
// because these are the cleanest dependencies // because these are the cleanest dependencies
compiler.observer.emit('get', key) compiler.observer.emit('get', key)

View File

@ -30,7 +30,7 @@ module.exports = {
focus: function (value) { focus: function (value) {
var el = this.el var el = this.el
setTimeout(function () { setTimeout(function () {
el[value ? 'focus' : 'focus']() if (value) el.focus()
}, 0) }, 0)
}, },

View File

@ -64,7 +64,7 @@ module.exports = {
if (compiler.each) { if (compiler.each) {
e.item = vm[compiler.eachPrefix] e.item = vm[compiler.eachPrefix]
} }
handler.call(vm, e) handler.call(ownerVM, e)
} }
this.el.addEventListener(event, this.handler) this.el.addEventListener(event, this.handler)

View File

@ -1,6 +1,4 @@
/* // Variable extraction scooped from https://github.com/RubyLouvre/avalon
* Variable extraction scooped from https://github.com/RubyLouvre/avalon
*/
var KEYWORDS = var KEYWORDS =
// keywords // keywords
'break,case,catch,continue,debugger,default,delete,do,else,false' 'break,case,catch,continue,debugger,default,delete,do,else,false'
@ -32,7 +30,13 @@ function getVariables (code) {
} }
module.exports = { 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) var vars = getVariables(exp)
if (!vars.length) return null if (!vars.length) return null
var args = [], var args = [],
@ -40,9 +44,15 @@ module.exports = {
hash = {} hash = {}
while (i--) { while (i--) {
v = vars[i] v = vars[i]
// avoid duplicate keys
if (hash[v]) continue if (hash[v]) continue
hash[v] = 1 hash[v] = 1
// push assignment
args.push(v + '=this.$get("' + v + '")') 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 args = 'var ' + args.join(',') + ';return ' + exp
/* jshint evil: true */ /* jshint evil: true */