Skip to content

Commit 354f559

Browse files
author
Evan You
committed
vm.$emit() is now self only, propagating events now triggered by $dispatch()
1 parent 303cacc commit 354f559

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/viewmodel.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,20 @@ def(VMProto, '$broadcast', function () {
8585
/**
8686
* emit an event that propagates all the way up to parent VMs.
8787
*/
88-
def(VMProto, '$emit', function () {
88+
def(VMProto, '$dispatch', function () {
8989
var compiler = this.$compiler,
9090
emitter = compiler.emitter,
9191
parent = compiler.parentCompiler
9292
emitter.emit.apply(emitter, arguments)
9393
if (parent) {
94-
parent.vm.$emit.apply(parent.vm, arguments)
94+
parent.vm.$dispatch.apply(parent.vm, arguments)
9595
}
9696
})
9797

9898
/**
9999
* delegate on/off/once to the compiler's emitter
100100
*/
101-
;['on', 'off', 'once'].forEach(function (method) {
101+
;['emit', 'on', 'off', 'once'].forEach(function (method) {
102102
def(VMProto, '$' + method, function () {
103103
var emitter = this.$compiler.emitter
104104
emitter[method].apply(emitter, arguments)

test/unit/specs/viewmodel.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ describe('UNIT: ViewModel', function () {
167167

168168
})
169169

170+
describe('$emit', function () {
171+
172+
it('should trigger the event', function () {
173+
var t = new Vue(),
174+
triggered = false
175+
t.$compiler.emitter.on('test', function (m) {
176+
triggered = m
177+
})
178+
t.$emit('test', 'hi')
179+
assert.strictEqual(triggered, 'hi')
180+
})
181+
182+
})
183+
170184
describe('.$broadcast()', function () {
171185

172186
it('should notify all child VMs', function () {
@@ -193,7 +207,7 @@ describe('UNIT: ViewModel', function () {
193207

194208
})
195209

196-
describe('.$emit', function () {
210+
describe('.$dispatch', function () {
197211

198212
it('should notify all ancestor VMs', function (done) {
199213
var topTriggered = false,
@@ -203,7 +217,7 @@ describe('UNIT: ViewModel', function () {
203217
ready: function () {
204218
var self = this
205219
nextTick(function () {
206-
self.$emit('hello', msg)
220+
self.$dispatch('hello', msg)
207221
assert.ok(topTriggered)
208222
assert.ok(midTriggered)
209223
done()

0 commit comments

Comments
 (0)