Ver Fonte

- add functional test for validation using filters
- filter functions now have a `this` context of the VM calling it.

Evan You há 12 anos atrás
pai
commit
15d54e9fdc

+ 1 - 1
src/directive.js

@@ -174,7 +174,7 @@ DirProto.applyFilters = function (value) {
     var filtered = value, filter
     for (var i = 0, l = this.filters.length; i < l; i++) {
         filter = this.filters[i]
-        filtered = filter.apply(filtered, filter.args)
+        filtered = filter.apply.call(this.vm, filtered, filter.args)
     }
     return filtered
 }

+ 38 - 0
test/functional/fixtures/validation.html

@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <title></title>
+        <meta charset="utf-8">
+        <script src="bind.js"></script>
+        <script src="../../../dist/seed.js"></script>
+        <style type="text/css">
+            input:not(.valid) {
+                outline-color: #f00;
+            }
+        </style>
+    </head>
+    <body>
+        <div id="test">
+            email: <input type="text" sd-model="a | email" sd-class="valid:validation.email">
+        </div>
+        <script>
+            Seed.config({debug: true})
+            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,}))$/
+            var test = new Seed({
+                el: '#test',
+                filters: {
+                    email: function (val) {
+                        this.validation.email = val === '' || RE.test(val)
+                        return val
+                    }
+                },
+                scope: {
+                    a: 'hihi',
+                    validation: {
+                        email: true
+                    }
+                }
+            })
+        </script>
+    </body>
+</html>

+ 13 - 0
test/functional/specs/validation.js

@@ -0,0 +1,13 @@
+casper.test.begin('Validation', 2, function (test) {
+    
+    casper
+    .start('./fixtures/validation.html', function () {
+        test.assertElementCount('.valid', 0)
+        this.sendKeys('input', '@hello.com')
+        test.assertElementCount('.valid', 1)
+    })
+    .run(function () {
+        test.done()
+    })
+
+})