Skip to content

Commit

Permalink
simplify v-repeat syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 23, 2013
1 parent 93b0325 commit cd90e64
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 50 deletions.
16 changes: 8 additions & 8 deletions examples/todomvc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@ <h1>todos</h1>
<ul id="todo-list">
<li
class="todo"
v-repeat="todo:todos"
v-if="todoFilter(todo.completed)"
v-repeat="todos"
v-if="todoFilter(completed)"
v-class="
completed : todo.completed,
editing : todo == editedTodo
completed : completed,
editing : this == editedTodo
"
>
<div class="view">
<input
class="toggle"
type="checkbox"
v-model="todo.completed"
v-model="completed"
v-on="change:toggleTodo"
>
<label v-text="todo.title" v-on="dblclick:editTodo"></label>
<label v-text="title" v-on="dblclick:editTodo"></label>
<button class="destroy" v-on="click:removeTodo"></button>
</div>
<input
class="edit"
type="text"
v-model="todo.title"
v-todo-focus="todo == editedTodo"
v-model="title"
v-todo-focus="this == editedTodo"
v-on="
blur : doneEdit,
keyup : doneEdit | key enter,
Expand Down
18 changes: 10 additions & 8 deletions examples/todomvc/js/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Vue.config({debug:true})

var filters = {
all: function () { return true },
active: function (completed) { return !completed },
Expand Down Expand Up @@ -43,32 +45,32 @@ var app = new Vue({
},

removeTodo: function (e) {
this.todos.remove(e.item)
this.remaining -= e.item.completed ? 0 : 1
this.todos.remove(e.targetVM.$scope)
this.remaining -= e.targetVM.completed ? 0 : 1
todoStorage.save()
},

toggleTodo: function (e) {
this.remaining += e.item.completed ? -1 : 1
this.remaining += e.targetVM.completed ? -1 : 1
todoStorage.save()
},

editTodo: function (e) {
this.beforeEditCache = e.item.title
this.editedTodo = e.item
this.beforeEditCache = e.targetVM.title
this.editedTodo = e.targetVM
},

doneEdit: function (e) {
if (!this.editedTodo) return
this.editedTodo = null
e.item.title = e.item.title.trim()
if (!e.item.title) this.removeTodo(e)
e.targetVM.title = e.targetVM.title.trim()
if (!e.targetVM.title) this.removeTodo(e)
todoStorage.save()
},

cancelEdit: function (e) {
this.editedTodo = null
e.item.title = this.beforeEditCache
e.targetVM.title = this.beforeEditCache
},

removeCompleted: function () {
Expand Down
8 changes: 6 additions & 2 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ function Compiler (vm, options) {
// for repeated items, create an index binding
// which should be inenumerable but configurable
if (compiler.repeat) {
scope.$index = compiler.repeatIndex
//scope.$index = compiler.repeatIndex
def(scope, '$index', compiler.repeatIndex, false, true)
compiler.createBinding('$index')
}

Expand Down Expand Up @@ -463,10 +464,13 @@ CompilerProto.define = function (key, binding) {
compiler.markComputed(binding)
}

if (!(key in scope)) {
// $index is inenumerable
if (!(key in scope) && key !== '$index') {
scope[key] = undefined
}

// if the scope object is already observed, that means
// this binding is created late. we need to observe it now.
if (scope.__observer__) {
Observer.convert(scope, key)
}
Expand Down
8 changes: 2 additions & 6 deletions src/directives/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ module.exports = {
var target = delegateCheck(e.target, delegator, identifier)
if (target) {
e.el = target
e.vm = target.vue_viewmodel
e.item = e.vm.$scope[compiler.repeatPrefix]
e.targetVM = target.vue_viewmodel
handler.call(ownerVM, e)
}
}
Expand All @@ -64,10 +63,7 @@ module.exports = {
var vm = this.vm
this.handler = function (e) {
e.el = e.currentTarget
e.vm = vm
if (compiler.repeat) {
e.item = vm.$scope[compiler.repeatPrefix]
}
e.targetVM = vm
handler.call(ownerVM, e)
}
this.el.addEventListener(event, this.handler)
Expand Down
11 changes: 3 additions & 8 deletions src/directives/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ var mutationHandlers = {
},

sort: function () {
var key = this.arg,
vms = this.vms,
var vms = this.vms,
col = this.collection,
l = col.length,
sorted = new Array(l),
Expand All @@ -61,7 +60,7 @@ var mutationHandlers = {
data = col[i]
for (j = 0; j < l; j++) {
vm = vms[j]
if (vm.$scope[key] === data) {
if (vm.$scope === data) {
sorted[i] = vm
break
}
Expand Down Expand Up @@ -153,7 +152,6 @@ module.exports = {

var node = this.el.cloneNode(true),
ctn = this.container,
scope = {},
ref, item

// append node into DOM first
Expand All @@ -171,16 +169,13 @@ module.exports = {
}, this.compiler)
}

// set data on scope and compile
scope[this.arg] = data || {}
item = new this.ChildVM({
el: node,
scope: scope,
scope: data,
compilerOptions: {
repeat: true,
repeatIndex: index,
repeatCollection: this.collection,
repeatPrefix: this.arg,
parentCompiler: this.compiler,
delegator: ctn
}
Expand Down
6 changes: 3 additions & 3 deletions test/functional/fixtures/nested-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<body>
<div id="test">
<ul>
<li v-repeat="item : items" v-class="'list-' + $index">
<li v-repeat="items" v-class="'list-' + $index">
<ul>
<li v-repeat="subitem : item.items" v-class="'list-' + $index">
{{$parent.$index + '.' + $index + ' : ' + item.title + '&lt;-' + subitem.title}}
<li v-repeat="items" v-class="'list-' + $index">
{{$parent.$index + '.' + $index + ' : ' + $parent.title + '&lt;-' + title}}
</li>
</ul>
</li>
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/functional/fixtures/repeated-items.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
</p>
<p>Total items: <span class="count" v-text="items.length"></span></p>
<ul>
<li class="item" v-repeat="item:items">
{{$index}} {{item.title}}
<li class="item" v-repeat="items">
{{$index}} {{title}}
</li>
</ul>
</div>
Expand Down
8 changes: 4 additions & 4 deletions test/functional/fixtures/repeated-vms.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
<script src="../../../dist/vue.js"></script>
</head>
<body>
<div class="item" v-repeat="item:items" v-component="item" v-on="click:click">
{{msg + ' ' + item.title}}
<div class="item" v-repeat="items" v-component="item" v-on="click:click">
{{msg + ' ' + title}}
</div>
<script>
Vue.config({ debug: true })

Vue.component('item', {
ready: function () {
this.item.title += ' init'
this.title += ' init'
},
proto: {
click: function () {
this.item.title += ' click'
this.title += ' click'
}
},
scope: {
Expand Down
16 changes: 8 additions & 8 deletions test/functional/fixtures/transition.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
</div>
<div
class="test"
v-repeat="item:items"
v-if="filter(item)"
v-repeat="items"
v-if="filter(this)"
v-transition
v-attr="data-id:item.a">
{{item.a}}
v-attr="data-id:a">
{{a}}
</div>
<div
class="test"
v-repeat="item:items"
v-show="filter(item)"
v-repeat="items"
v-show="filter(this)"
v-transition
v-attr="data-id:item.a">
{{item.a}}
v-attr="data-id:a">
{{a}}
</div>
</div>
<h1 style="margin:0">123</h1>
Expand Down
2 changes: 1 addition & 1 deletion test/functional/specs/nested-vms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
casper.test.begin('Nested Viewmodels', 7, function (test) {

casper
.start('./fixtures/nested-viewmodels.html', function () {
.start('./fixtures/nested-vms.html', function () {

test.assertSelectorHasText('.ancestor', 'Andy Johnson')
test.assertSelectorHasText('.jack', 'Jack, son of Andy')
Expand Down

0 comments on commit cd90e64

Please sign in to comment.