From bfed0298a5d29d9ef8afe708c1766f93ca61f3d2 Mon Sep 17 00:00:00 2001 From: zhangquan Date: Thu, 22 Sep 2016 14:07:15 +0800 Subject: [PATCH] * [jsfm] fix #1237, fix binding event bug of components which have repeat attribute --- html5/default/vm/directive.js | 3 +- html5/test/unit/default/vm/events.js | 56 +++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/html5/default/vm/directive.js b/html5/default/vm/directive.js index 1d8aa77a8e..39e8bd399f 100644 --- a/html5/default/vm/directive.js +++ b/html5/default/vm/directive.js @@ -254,7 +254,8 @@ function bindEvents (vm, el, events) { console.debug(`[JS Framework] The method "${handler}" is not defined.`) } } - setEvent(vm, el, key, handler) + const realVm = vm._realParent ? vm._realParent : vm + setEvent(realVm, el, key, handler) } } diff --git a/html5/test/unit/default/vm/events.js b/html5/test/unit/default/vm/events.js index b52e975675..af262e8cf5 100644 --- a/html5/test/unit/default/vm/events.js +++ b/html5/test/unit/default/vm/events.js @@ -59,7 +59,6 @@ describe('bind and fire events', () => { checkReady(vm, function () { doc.close() - expect(doc.body.event.click).a('function') const el = doc.body @@ -79,6 +78,61 @@ describe('bind and fire events', () => { }) }) + it('context of event binding to element width repeat attribute', (done) => { + customComponentMap.foo = { + template: { + type: 'div', + id: 'container', + events: { + click: 'containerClick' + }, + children: [ + { + type: 'div', + id: 'items', + 'repeat': function () { return this.items }, + events: { + click: 'itemsClick' + }, + children: [ + { + type: 'div' + } + ] + } + ] + }, + data: function () { + return { + items: [ + { name: 1 }, + { name: 2 } + ] + } + }, + methods: { + containerClick: function () { + expect(this).eql(vm) + }, + itemsClick: function () { + expect(this).eql(vm) + } + } + } + + const app = { doc, customComponentMap } + const vm = new Vm('foo', customComponentMap.foo, { _app: app }) + + checkReady(vm, function () { + doc.close() + const containerEl = vm.$el('container') + const itemsEl = vm.$el('items') + containerEl.event.click() + itemsEl.event.click() + done() + }) + }) + it('emit, broadcast and dispatch vm events', (done) => { customComponentMap.foo = { template: {