Ver código fonte

vm.$emit() is now self only, propagating events now triggered by $dispatch()

Evan You 12 anos atrás
pai
commit
354f559215
2 arquivos alterados com 19 adições e 5 exclusões
  1. 3 3
      src/viewmodel.js
  2. 16 2
      test/unit/specs/viewmodel.js

+ 3 - 3
src/viewmodel.js

@@ -85,20 +85,20 @@ def(VMProto, '$broadcast', function () {
 /**
  *  emit an event that propagates all the way up to parent VMs.
  */
-def(VMProto, '$emit', function () {
+def(VMProto, '$dispatch', function () {
     var compiler = this.$compiler,
         emitter = compiler.emitter,
         parent = compiler.parentCompiler
     emitter.emit.apply(emitter, arguments)
     if (parent) {
-        parent.vm.$emit.apply(parent.vm, arguments)
+        parent.vm.$dispatch.apply(parent.vm, arguments)
     }
 })
 
 /**
  *  delegate on/off/once to the compiler's emitter
  */
-;['on', 'off', 'once'].forEach(function (method) {
+;['emit', 'on', 'off', 'once'].forEach(function (method) {
     def(VMProto, '$' + method, function () {
         var emitter = this.$compiler.emitter
         emitter[method].apply(emitter, arguments)

+ 16 - 2
test/unit/specs/viewmodel.js

@@ -167,6 +167,20 @@ describe('UNIT: ViewModel', function () {
 
     })
 
+    describe('$emit', function () {
+        
+        it('should trigger the event', function () {
+            var t = new Vue(),
+                triggered = false
+            t.$compiler.emitter.on('test', function (m) {
+                triggered = m
+            })
+            t.$emit('test', 'hi')
+            assert.strictEqual(triggered, 'hi')
+        })
+
+    })
+
     describe('.$broadcast()', function () {
         
         it('should notify all child VMs', function () {
@@ -193,7 +207,7 @@ describe('UNIT: ViewModel', function () {
 
     })
 
-    describe('.$emit', function () {
+    describe('.$dispatch', function () {
         
         it('should notify all ancestor VMs', function (done) {
             var topTriggered = false,
@@ -203,7 +217,7 @@ describe('UNIT: ViewModel', function () {
                 ready: function () {
                     var self = this
                     nextTick(function () {
-                        self.$emit('hello', msg)
+                        self.$dispatch('hello', msg)
                         assert.ok(topTriggered)
                         assert.ok(midTriggered)
                         done()