From 3941960681773e581f4a681cc7793bdc749f2ceb Mon Sep 17 00:00:00 2001 From: dntzhang Date: Sat, 10 Nov 2018 08:17:06 +0800 Subject: [PATCH] omi-mp - fix triggerEvent && directory compatible with mac os and window --- packages/omi-mp/gulpfile.js | 15 +++++++++++++-- .../src/mp/components/my-child/my-child.js | 15 +++++++++++++-- .../src/mp/components/my-child/my-child.wxml | 2 +- .../omi-mp/src/mp/components/my-ele/my-ele.js | 17 ++++++++++++++--- .../omi-mp/src/mp/components/my-ele/my-ele.wxml | 4 ++-- packages/omi-mp/src/mp/pages/logs/logs.js | 8 ++++++-- packages/omi-mp/src/mp/pages/logs/logs.wxml | 2 +- 7 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/omi-mp/gulpfile.js b/packages/omi-mp/gulpfile.js index 38a9441e2..fe537f360 100644 --- a/packages/omi-mp/gulpfile.js +++ b/packages/omi-mp/gulpfile.js @@ -11,7 +11,7 @@ gulp.task('components', ['copy'], () => { .pipe( tap(file => { let dir = path.dirname(file.path) - let arr = dir.split('\\') + let arr = dir.split(/\\|\//) let name = arr[arr.length-1] let wxml = fs.readFileSync(dir + '/' + name + '.wxml', 'utf8') let json = require(dir + '/' + name + '.json') @@ -50,7 +50,14 @@ class Element extends WeElement { afterUpdate() {} - install = mpOption.created || function() {} + install = function() { + mpOption.created && mpOption.created.call(this) + Object.keys(mpOption.methods).forEach(key => { + if(typeof mpOption.methods[key] === 'function'){ + this[key] = mpOption.methods[key].bind(this) + } + }) + } uninstall = mpOption.detached || function() {} @@ -59,6 +66,10 @@ class Element extends WeElement { mpOption.ready && mpOption.ready.call(this) } + triggerEvent = function(name, data) { + this.fire(name, data) + } + setData = setData } diff --git a/packages/omi-mp/src/mp/components/my-child/my-child.js b/packages/omi-mp/src/mp/components/my-child/my-child.js index f661bea50..1aad6b01d 100644 --- a/packages/omi-mp/src/mp/components/my-child/my-child.js +++ b/packages/omi-mp/src/mp/components/my-child/my-child.js @@ -9,7 +9,7 @@ function css() { } function render() { - return h('span',null,[`my-child`]) + return h('span',null,[`I am child component`]) } // components/my-child/my-child.js @@ -49,7 +49,14 @@ class Element extends WeElement { afterUpdate() {} - install = mpOption.created || function() {} + install = function() { + mpOption.created && mpOption.created.call(this) + Object.keys(mpOption.methods).forEach(key => { + if(typeof mpOption.methods[key] === 'function'){ + this[key] = mpOption.methods[key].bind(this) + } + }) + } uninstall = mpOption.detached || function() {} @@ -58,6 +65,10 @@ class Element extends WeElement { mpOption.ready && mpOption.ready.call(this) } + triggerEvent = function(name, data) { + this.fire(name, data) + } + setData = setData } diff --git a/packages/omi-mp/src/mp/components/my-child/my-child.wxml b/packages/omi-mp/src/mp/components/my-child/my-child.wxml index 412be0ae9..d1d363b34 100644 --- a/packages/omi-mp/src/mp/components/my-child/my-child.wxml +++ b/packages/omi-mp/src/mp/components/my-child/my-child.wxml @@ -1,2 +1,2 @@ -my-child +I am child component diff --git a/packages/omi-mp/src/mp/components/my-ele/my-ele.js b/packages/omi-mp/src/mp/components/my-ele/my-ele.js index 4b4babbbb..06a428947 100644 --- a/packages/omi-mp/src/mp/components/my-ele/my-ele.js +++ b/packages/omi-mp/src/mp/components/my-ele/my-ele.js @@ -9,7 +9,7 @@ function css() { } function render() { - return h('div',null,[h('my-child',null,[]),h('div',{'ontap': this.myMethods},[`my-ele`])]) + return h('div',null,[h('button',{'ontap': this.myMethods},[`Click me will log dntzhang to the console panel`]),h('my-child',null,[])]) } // components/my-ele/my-ele.js @@ -33,7 +33,7 @@ const mpOption = Component({ */ methods: { myMethods: function() { - console.log('myMethods') + this.triggerEvent('myevent', { name: 'dntzhang' }) } } }) @@ -51,7 +51,14 @@ class Element extends WeElement { afterUpdate() {} - install = mpOption.created || function() {} + install = function() { + mpOption.created && mpOption.created.call(this) + Object.keys(mpOption.methods).forEach(key => { + if(typeof mpOption.methods[key] === 'function'){ + this[key] = mpOption.methods[key].bind(this) + } + }) + } uninstall = mpOption.detached || function() {} @@ -60,6 +67,10 @@ class Element extends WeElement { mpOption.ready && mpOption.ready.call(this) } + triggerEvent = function(name, data) { + this.fire(name, data) + } + setData = setData } diff --git a/packages/omi-mp/src/mp/components/my-ele/my-ele.wxml b/packages/omi-mp/src/mp/components/my-ele/my-ele.wxml index 9317700d0..5953b15de 100644 --- a/packages/omi-mp/src/mp/components/my-ele/my-ele.wxml +++ b/packages/omi-mp/src/mp/components/my-ele/my-ele.wxml @@ -1,4 +1,4 @@ - - my-ele + + diff --git a/packages/omi-mp/src/mp/pages/logs/logs.js b/packages/omi-mp/src/mp/pages/logs/logs.js index 3196fa0ed..830513ef7 100644 --- a/packages/omi-mp/src/mp/pages/logs/logs.js +++ b/packages/omi-mp/src/mp/pages/logs/logs.js @@ -9,9 +9,9 @@ function css() { } function render() { const { logs } = Object.assign({}, this.data, this.props) - return h('div',{'class': `container log-list`},[ logs.map((log,index)=>{ + return h('div',{'class': `container log-list `},[ logs.map((log,index)=>{ return h('span',{'class': `log-item`},[`${index + 1}. ${log}`]) - }),h('my-ele',null,[])]) + }),h('my-ele',{'onmyevent': this.myEventHandler},[])]) } //logs.js @@ -27,6 +27,10 @@ const mpOption = Page({ return util.formatTime(new Date(log)) }) }) + }, + myEventHandler: function (evt) { + //output -> dntzhang + console.log(evt.detail.name) } }) diff --git a/packages/omi-mp/src/mp/pages/logs/logs.wxml b/packages/omi-mp/src/mp/pages/logs/logs.wxml index 798955184..220e092d5 100644 --- a/packages/omi-mp/src/mp/pages/logs/logs.wxml +++ b/packages/omi-mp/src/mp/pages/logs/logs.wxml @@ -3,5 +3,5 @@ {{index + 1}}. {{log}} - +