Skip to content

Commit 15d54e9

Browse files
author
Evan You
committed
- add functional test for validation using filters
- filter functions now have a `this` context of the VM calling it.
1 parent 1270139 commit 15d54e9

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/directive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ DirProto.applyFilters = function (value) {
174174
var filtered = value, filter
175175
for (var i = 0, l = this.filters.length; i < l; i++) {
176176
filter = this.filters[i]
177-
filtered = filter.apply(filtered, filter.args)
177+
filtered = filter.apply.call(this.vm, filtered, filter.args)
178178
}
179179
return filtered
180180
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title></title>
5+
<meta charset="utf-8">
6+
<script src="bind.js"></script>
7+
<script src="../../../dist/seed.js"></script>
8+
<style type="text/css">
9+
input:not(.valid) {
10+
outline-color: #f00;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<div id="test">
16+
email: <input type="text" sd-model="a | email" sd-class="valid:validation.email">
17+
</div>
18+
<script>
19+
Seed.config({debug: true})
20+
var RE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
21+
var test = new Seed({
22+
el: '#test',
23+
filters: {
24+
email: function (val) {
25+
this.validation.email = val === '' || RE.test(val)
26+
return val
27+
}
28+
},
29+
scope: {
30+
a: 'hihi',
31+
validation: {
32+
email: true
33+
}
34+
}
35+
})
36+
</script>
37+
</body>
38+
</html>

test/functional/specs/validation.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
casper.test.begin('Validation', 2, function (test) {
2+
3+
casper
4+
.start('./fixtures/validation.html', function () {
5+
test.assertElementCount('.valid', 0)
6+
this.sendKeys('input', '@hello.com')
7+
test.assertElementCount('.valid', 1)
8+
})
9+
.run(function () {
10+
test.done()
11+
})
12+
13+
})

0 commit comments

Comments
 (0)