Skip to content

Commit

Permalink
feat: support dom event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangdaiyan committed Apr 18, 2019
1 parent 93ba39e commit 2f9297b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

4. [html原生元素渲染](https://github.com/zzz945/write-vue3-from-scratch/commit/89df7464fec10653b2e12e4cb42756d71312a5dd)

5. [支持method]

6. [支持lifecycle]

7. [事件监听]

5. Component vnode render (TODO)

6. Handle dom event (TODO)
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class Vue {
el.setAttribute(key, vnode.data[key]);
}

// set dom eventlistener
const events = (vnode.data || {}).on || {}
for (let key in events) {
el.addEventListener(key, events[key])
}

if (typeof vnode.children === 'string') {
el.textContent = vnode.children
} else {
Expand All @@ -50,7 +56,7 @@ class Vue {
}

return el
}
}
initDataProxy () {
const data = this.$options.data ? this.$options.data() : {}

Expand Down
22 changes: 22 additions & 0 deletions test/event-listen.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Vue from "../src/index.js";

describe('Event test', function() {
it('Basic', function() {
var cb = jasmine.createSpy('cb');

var vm = new Vue({
render (h) {
return h('button', {
class: 'btn',
on: { 'click': cb }
}, [])
},
}).$mount()

document.body.appendChild(vm.$el)
const btn = document.querySelector('.btn')
expect(btn.tagName).toEqual('BUTTON')
btn.click()
expect(cb).toHaveBeenCalled()
});
});

0 comments on commit 2f9297b

Please sign in to comment.