Просмотр исходного кода

$dispatch: should also trigger event on self

Evan You 10 лет назад
Родитель
Сommit
b4a3a63fb7
2 измененных файлов с 3 добавлено и 1 удалено
  1. 1 0
      src/api/events.js
  2. 2 1
      test/unit/specs/api/events_spec.js

+ 1 - 0
src/api/events.js

@@ -134,6 +134,7 @@ exports.$broadcast = function (event) {
  */
 
 exports.$dispatch = function () {
+  this.$emit.apply(this, arguments)
   var parent = this.$parent
   while (parent) {
     parent.$emit.apply(parent, arguments)

+ 2 - 1
test/unit/specs/api/events_spec.js

@@ -134,10 +134,11 @@ describe('Events API', function () {
   it('$dispatch', function () {
     var child = vm.$addChild()
     var child2 = child.$addChild()
+    child2.$on('test', spy)
     child.$on('test', spy)
     vm.$on('test', spy)
     child2.$dispatch('test')
-    expect(spy.calls.count()).toBe(1) // should not propagate by default
+    expect(spy.calls.count()).toBe(2) // should trigger on self, but not propagate to root
   })
 
   it('$dispatch with propagation', function () {