فهرست منبع

vm.$watch: support filters

Evan You 10 سال پیش
والد
کامیت
832f6889a2
2فایلهای تغییر یافته به همراه20 افزوده شده و 4 حذف شده
  1. 10 4
      src/api/data.js
  2. 10 0
      test/unit/specs/api/data_spec.js

+ 10 - 4
src/api/data.js

@@ -59,7 +59,7 @@ exports.$delete = function (key) {
  * Watch an expression, trigger callback when its
  * Watch an expression, trigger callback when its
  * value changes.
  * value changes.
  *
  *
- * @param {String} exp
+ * @param {String|Function} expOrFn
  * @param {Function} cb
  * @param {Function} cb
  * @param {Object} [options]
  * @param {Object} [options]
  *                 - {Boolean} deep
  *                 - {Boolean} deep
@@ -68,11 +68,17 @@ exports.$delete = function (key) {
  * @return {Function} - unwatchFn
  * @return {Function} - unwatchFn
  */
  */
 
 
-exports.$watch = function (exp, cb, options) {
+exports.$watch = function (expOrFn, cb, options) {
   var vm = this
   var vm = this
-  var watcher = new Watcher(vm, exp, cb, {
+  var parsed
+  if (typeof expOrFn === 'string') {
+    parsed = dirParser.parse(expOrFn)
+    expOrFn = parsed.expression
+  }
+  var watcher = new Watcher(vm, expOrFn, cb, {
     deep: options && options.deep,
     deep: options && options.deep,
-    user: !options || options.user !== false
+    user: !options || options.user !== false,
+    filters: parsed && parsed.filters
   })
   })
   if (options && options.immediate) {
   if (options && options.immediate) {
     cb.call(vm, watcher.value)
     cb.call(vm, watcher.value)

+ 10 - 0
test/unit/specs/api/data_spec.js

@@ -137,6 +137,16 @@ describe('Data API', function () {
     })
     })
   })
   })
 
 
+  it('$watch with filters', function (done) {
+    var spy = jasmine.createSpy()
+    vm.$watch('a | double', spy)
+    vm.a = 2
+    nextTick(function () {
+      expect(spy).toHaveBeenCalledWith(4, 2)
+      done()
+    })
+  })
+
   it('$eval', function () {
   it('$eval', function () {
     expect(vm.$eval('a')).toBe(1)
     expect(vm.$eval('a')).toBe(1)
     expect(vm.$eval('b.c')).toBe(2)
     expect(vm.$eval('b.c')).toBe(2)