Skip to content

Commit 607e197

Browse files
committed
computed filters + test
1 parent 4ebff99 commit 607e197

File tree

7 files changed

+69
-8
lines changed

7 files changed

+69
-8
lines changed

examples/todomvc/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>todos</h1>
2828
<ul id="todo-list">
2929
<li
3030
class="todo"
31-
v-repeat="filteredTodos"
31+
v-repeat="todos | filterTodos"
3232
v-class="
3333
completed : completed,
3434
editing : this == editedTodo

examples/todomvc/js/app.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@
4444
}
4545
},
4646

47+
filters: {
48+
filterTodos: function (todos) {
49+
return todos.filter(filters[this.filter]);
50+
}
51+
},
52+
4753
// computed property
4854
// http://vuejs.org/guide/computed.html
4955
computed: {
50-
filteredTodos: function () {
51-
return this.todos.filter(filters[this.filter]);
52-
},
5356
remaining: function () {
54-
return this.todos.filter(filters.active).length
57+
return this.todos.filter(filters.active).length;
5558
},
5659
allDone: {
5760
$get: function () {

src/exp-parser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ exports.parse = function (exp, compiler, data, filters) {
177177
return strings[i]
178178
}
179179

180-
console.log(body)
181-
182180
return makeGetter(body, exp)
183181
}
184182

src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ assetTypes.forEach(function (type) {
3131
value = utils.toFragment(value)
3232
} else if (type === 'component') {
3333
value = utils.toConstructor(value)
34+
} else if (type === 'filter') {
35+
utils.checkFilter(value)
3436
}
3537
hash[id] = value
3638
return this

src/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var config = require('./config'),
33
win = window,
44
console = win.console,
55
timeout = win.setTimeout,
6+
THIS_RE = /[^\w]this\./,
67
hasClassList = 'classList' in document.documentElement,
78
ViewModel // late def
89

@@ -164,13 +165,24 @@ var utils = module.exports = {
164165
: null
165166
},
166167

168+
/**
169+
* Check if a filter function contains references to `this`
170+
* If yes, mark it as a computed filter.
171+
*/
172+
checkFilter: function (filter) {
173+
if (THIS_RE.test(filter.toString())) {
174+
filter.computed = true
175+
}
176+
},
177+
167178
/**
168179
* convert certain option values to the desired format.
169180
*/
170181
processOptions: function (options) {
171182
var components = options.components,
172183
partials = options.partials,
173184
template = options.template,
185+
filters = options.filters,
174186
key
175187
if (components) {
176188
for (key in components) {
@@ -182,6 +194,11 @@ var utils = module.exports = {
182194
partials[key] = utils.toFragment(partials[key])
183195
}
184196
}
197+
if (filters) {
198+
for (key in filters) {
199+
utils.checkFilter(filters[key])
200+
}
201+
}
185202
if (template) {
186203
options.template = utils.toFragment(template)
187204
}

test/unit/specs/directive.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ describe('Directive', function () {
55

66
var compiler = {
77
options: {},
8-
getOption: function () {},
8+
getOption: function (type, id) {
9+
return Vue.options[type][id]
10+
},
911
vm: {
1012
constructor: {}
1113
}

test/unit/specs/misc.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,43 @@ describe('Misc Features', function () {
168168

169169
})
170170

171+
describe('computed filter', function () {
172+
173+
it('should process filter with `this` as computed', function (done) {
174+
175+
var V = Vue.extend({
176+
template: '<div class="plus">{{n | plus}}</div><div class="minus">{{n | minus}}</div>',
177+
filters: {
178+
plus: function (v) {
179+
return v + this.a
180+
}
181+
}
182+
})
183+
184+
V.filter('minus', function (v) {
185+
return v - this.a
186+
})
187+
188+
var v = new V({
189+
data: {
190+
a: 1,
191+
n: 1
192+
}
193+
})
194+
195+
assert.strictEqual(v.$el.querySelector('.plus').textContent, '2')
196+
assert.strictEqual(v.$el.querySelector('.minus').textContent, '0')
197+
198+
v.a = 2
199+
200+
nextTick(function () {
201+
assert.strictEqual(v.$el.querySelector('.plus').textContent, '3')
202+
assert.strictEqual(v.$el.querySelector('.minus').textContent, '-1')
203+
done()
204+
})
205+
206+
})
207+
208+
})
209+
171210
})

0 commit comments

Comments
 (0)