Skip to content

Commit

Permalink
vm.$emit() is now self only, propagating events now triggered by $dis…
Browse files Browse the repository at this point in the history
…patch()
  • Loading branch information
Evan You committed Jan 6, 2014
1 parent 303cacc commit 354f559
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/viewmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions test/unit/specs/viewmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 354f559

Please sign in to comment.