“快应用”是九大手机厂商联合推出的一个应用生态。这篇文章我想简单评价一下“快应用”中使用的一种事件广播机制。仅代表个人看法!

“快应用”中使用的的事件广播机制是怎样的

直接放文档中的话:

框架向开发者提供了双向的事件传递:
* 向下传递:父组件触发,子组件响应;调用parentVm.$broadcast()完成向下传递
* 向上传递:子组件触发,父组件响应;调用childVm.$dispath()完成向上传递

提示:
* 触发时传递参数,再接收时使用evt.detail来获取参数
* 当传递结束后,可以调用evt.stop()来结束传递,否则会一直传递下去

通过$on()监控自定义事件的触发

简而言之,组件可以沿着组件树向上或向下广播事件,然后在广播途中的组件可以进行事件监听。

我对这种机制的看法

初看到这种机制,差点以为穿越回了angularjs时代,它与angularjs中的事件广播机制是一致的。都9012年了,“快应用”也是8012年的框架,我搞不懂它为什么要加入这样一种机制。

我认为使用这个机制是非常难受的,想象一下下面几个场景:
1. 在一个组件中有这样一个事件监听$on('xxx'),然后你看到它会一脸懵逼,你不知道这个事件是从它的祖先传过来的,还是从它的后代传过来的,更无法精确定位触发事件的组件。然后你挨个组件找了半天,终于把触发事件的组件找到了。
2. 父组件有多个相同的子组件,甚至这几个子组件不在同一层级上,这个子组件里面$dispatch('xxx'),当触发事件的时候你又懵逼了,这个事件到底是谁触发的。
3. 你和你的同事把事件名字给写重了,出bug时你的懵逼程度就无法想象了。

这个机制大大增加了组件之间的耦合度,使得程序变得难以理解,难以维护。尤其是随着应用体量的增大,还有到应用的维护阶段,或者开发人员的更换,它会让你觉得这简直就是一场噩梦。

我们应该如何使用它

谨慎。

能不用就不用。

当然,并不是一定不能用的,有时使用这种机制还是很方便的,例如全局数据的更新(onUserDataLoad)。如果你一定要使用它的话,最好有详细的注释、完整的文档,你的开发小组最好规定一些使用规范及命名规则。

但是,规范是不可能被遵守的,如果它不是强制的话。所以我还是衷心希望“快应用”如果有下次大版本更新,还是把这个机制去掉的好。

与浏览器事件相比

浏览器事件的冒泡和捕获也是使用的这种事件广播机制,但是浏览器事件是有明确的定义的,触发是由用户触发的,它本身就是适合于这种机制的。

与发布订阅模式对比

如果不使用“快应用”提供的这种机制,那可以使用什么方法来实现功能呢?一种方式就是使用发布订阅模式。

你可以实现一个事件中心,虽然也有可能会导致事件的混乱,但好处是可以明确地知道应用中有哪些事件,可以方便地管理这些事件。

当然也可以直接使用像redux这样的状态管理器。我没有试过在“快应用”中使用redux,但想必是很好用的。

与观察者模式对比

另一种替代方式是观察者模式。

可以自己实现或直接使用RXjs。好处是可以明确地知道谁是数据来源(Observable)。

$emit

当然子组件向父组件传递数据最好还是使用$emit。

但是说到$emit槽点又来了。“快应用”文档中对$emit触发的事件如何注册监听描述基本是对不上实际使用方式的。而且事件监听时竟然没有事件对象的占位符,搞得我不得不这样写:

<aea @aea-click="aeaClickHandlerFactory('aaaaaa')()"></aea>
  aeaClickHandlerFactory(param) {
    console.log(param)
    return function (data) {
      prompt.showToast({ message: data })
    }
  }
分类: 前端

64 条评论

g these · 2020年6月20日 上午10:22

Hello, i feel that i noticed you visited my weblog
so i came to return the prefer?.I’m attempting to to
find issues to enhance my website!I assume
its ok to make use of a few of your ideas!!

and g · 2020年6月21日 上午4:30

Awesome things here. I’m very satisfied to look your post.
Thank you a lot and I am taking a look forward to touch you.

Will you please drop me a mail?

http://tinyurl.com/yalhd5ff · 2020年6月26日 下午3:48

Thanks very nice blog!

web.jmjh.tn.edu.tw · 2020年6月26日 下午5:14

Thank you for another informative website.
Where else could I get that kind of info written in such an ideal means?

I’ve a project that I am just now working on, and I have been on the look out for
such information.

http://tinyurl.com · 2020年6月27日 上午10:35

obviously like your web site but you have to take a look at the spelling on several of your posts.
Many of them are rife with spelling issues and I to find it very troublesome to tell
the reality nevertheless I’ll certainly come again again.

https://spcuniversity.privatesidesolutions.com · 2020年6月27日 下午1:42

Pretty nice post. I simply stumbled upon your blog and
wished to mention that I have really loved surfing around your weblog posts.

In any case I will be subscribing in your feed and I hope you write once more very soon!

cbd oil that works 2020 · 2020年6月27日 下午4:54

After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and
from now on each time a comment is added I receive four emails with the exact
same comment. Is there an easy method you can remove me from
that service? Thanks!

cbd oil that works 2020 · 2020年6月27日 下午5:01

I absolutely love your site.. Pleasant colors & theme.
Did you create this web site yourself? Please reply back as I’m wanting to create my very own blog and would love to learn where you got this from or just
what the theme is named. Thanks!

cbd oil that works 2020 · 2020年6月28日 上午2:36

Hey very cool site!! Man .. Beautiful .. Superb
.. I’ll bookmark your blog and take the feeds additionally?
I am satisfied to seek out so many helpful information here within the post, we need develop more strategies on this regard, thank you for sharing.

. . . . .

cbd oil that works 2020 · 2020年6月28日 上午5:32

Fantastic goods from you, man. I have have in mind
your stuff prior to and you are just extremely fantastic.

I really like what you’ve received right here,
really like what you are stating and the way during which you are saying it.
You’re making it enjoyable and you continue to take care of to keep it sensible.
I can not wait to learn far more from you. This is actually a great web site.

cbd oil that works 2020 · 2020年6月29日 下午12:42

Today, while I was at work, my cousin stole my iPad and tested to
see if it can survive a forty foot drop, just so she can be a youtube sensation. My
apple ipad is now broken and she has 83 views.
I know this is entirely off topic but I had to share
it with someone!

cbd oil that works 2020 · 2020年6月29日 下午5:44

This is very interesting, You’re a very professional blogger.
I have joined your rss feed and stay up for looking for extra of your great post.
Additionally, I’ve shared your site in my social networks

web hosting sites · 2020年7月18日 上午10:02

You actually make it appear so easy along
with your presentation but I to find this topic to be actually something that I feel I’d never understand.

It kind of feels too complex and extremely wide for me.
I’m looking forward in your next post, I will try to get the dangle of it!

web hosting service · 2020年7月18日 上午11:10

I feel this is one of the most important info for me.

And i’m glad studying your article. However should statement on few general issues,
The website style is great, the articles is actually excellent : D.
Just right activity, cheers

best web hosting sites · 2020年7月25日 上午10:04

I was curious if you ever thought of changing the page layout of your website?
Its very well written; I love what youve got to say. But maybe you could a little
more in the way of content so people could connect with it
better. Youve got an awful lot of text for only having 1 or two pictures.

Maybe you could space it out better?

best web hosting 2020 · 2020年7月26日 上午5:48

Hmm is anyone else experiencing problems with the images on this
blog loading? I’m trying to find out if its a problem on my end or
if it’s the blog. Any feed-back would be greatly appreciated.

flight ticket · 2020年7月28日 上午9:37

Magnificent beat ! I wish to apprentice while you amend your
web site, how could i subscribe for a blog web site?

The account aided me a acceptable deal. I had been a
little bit acquainted of this your broadcast offered bright clear idea

book flight · 2020年7月29日 下午3:15

I was able to find good info from your articles.

adreamoftrains website host · 2020年7月30日 下午8:52

Hello my loved one! I wish to say that this article is amazing, nice written and come
with almost all important infos. I’d like to see extra posts like
this . adreamoftrains website host

cheap flights with jet2.com · 2020年7月31日 上午9:05

Hello, I enjoy reading all of your article post. I like to write a little comment to support you.

best website hosting · 2020年8月5日 下午7:46

I was suggested this web site by means of my cousin. I’m not certain whether or not this publish is written by means of him as nobody else understand such specific approximately my problem.
You’re wonderful! Thank you!

best web hosting sites · 2020年8月5日 下午9:29

Thankfulness to my father who stated to me on the topic of this
weblog, this website is truly amazing.

web hosting sites · 2020年8月6日 下午5:59

These are actually fantastic ideas in about blogging.
You have touched some good factors here.

Any way keep up wrinting.

content hosting · 2020年8月6日 下午7:50

Wow, that’s what I was exploring for, what a data!
present here at this blog, thanks admin of this web site.

best web hosting sites · 2020年8月7日 下午12:34

I got this web page from my friend who informed me
about this website and at the moment this time I am browsing this web page and reading very informative posts
here.

web hosting services · 2020年8月7日 下午4:49

When someone writes an post he/she keeps the idea of a user in his/her brain that how a user can be aware
of it. Therefore that’s why this article is perfect.
Thanks!

web hosting providers · 2020年8月10日 上午11:17

I love your blog.. very nice colors & theme. Did you
make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to create my own blog and would
like to know where u got this from. thanks a lot

best web hosting 2020 · 2020年8月12日 上午2:04

Way cool! Some very valid points! I appreciate you penning this post and also the rest of the website is also very good.

website hosting services · 2020年8月14日 上午10:59

I believe that is among the most important information for me.
And i am glad reading your article. But should remark on some basic issues, The site taste is great, the articles is in reality great : D.
Excellent process, cheers

best web hosting company · 2020年8月14日 下午3:38

My brother recommended I may like this website. He was once entirely right.
This submit actually made my day. You can not imagine just
how so much time I had spent for this info! Thank you!

cheap flights · 2020年8月24日 下午3:45

Hi to every body, it’s my first pay a quick
visit of this webpage; this weblog contains remarkable and actually
fine stuff for visitors.

cheap flights · 2020年8月25日 上午12:45

Excellent blog here! Also your site loads
up fast! What host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as fast as yours lol cheap flights
2CSYEon

cheap flights · 2020年8月25日 上午5:16

It’s really a nice and helpful piece of information. I’m
glad that you just shared this helpful information with us.
Please keep us up to date like this. Thank you for sharing.
34pIoq5 cheap flights

cheap flights · 2020年8月25日 下午2:46

Have you ever thought about adding a little bit more than just your articles?
I mean, what you say is fundamental and everything.

However just imagine if you added some great visuals or video clips to give your posts more, “pop”!
Your content is excellent but with images and video clips, this site could
certainly be one of the very best in its field. Fantastic blog!
cheap flights 34pIoq5

web hosting services · 2020年8月25日 下午5:36

Pretty! This has been a really wonderful article. Thanks for supplying this info.

cheap flights · 2020年8月26日 下午11:03

Hello there! This blog post could not be written any better!
Reading through this article reminds me of my previous roommate!
He always kept preaching about this. I am going to forward this post to him.
Pretty sure he will have a good read. I appreciate you
for sharing!

cheap flights · 2020年8月28日 上午8:42

Hello there, I found your web site by way of Google even as searching for a related topic, your website got here up, it appears to be like
good. I’ve bookmarked it in my google bookmarks.

Hi there, simply changed into aware of your weblog via Google, and located that it’s truly informative.

I’m going to watch out for brussels. I will be grateful should you
continue this in future. Numerous folks will probably be benefited
from your writing. Cheers!

cheap flights · 2020年8月28日 下午1:03

This post is truly a nice one it helps new the web people,
who are wishing in favor of blogging.

black mass · 2020年8月28日 下午10:00

I’ve been browsing online more than 4 hours today, yet I
never found any interesting article like yours. It’s pretty worth enough for me.

In my view, if all web owners and bloggers made good content as you did, the net will be much more useful
than ever before.

black mass · 2020年8月28日 下午10:13

Wow, wonderful blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website is wonderful,
as well as the content!

web hosting companies · 2020年8月31日 下午8:08

This site was… how do you say it? Relevant!! Finally I have found something which helped me.
Cheers!

best website hosting · 2020年8月31日 下午8:29

I love what you guys are usually up too. This kind
of clever work and reporting! Keep up the terrific works guys I’ve included you guys to my own blogroll.

KevinJab · 2020年9月2日 下午9:41

ed medication online: buy ed drugs online

RichardFeaph · 2020年9月3日 下午2:02

price of viagra: viagra generic

RichardFeaph · 2020年9月4日 下午8:37

where to buy viagra online: viagra 100mg

Richardmab · 2020年9月6日 下午3:48

http://lasixtop.com/ – lasix furosemide 40 mg

Richardmab · 2020年9月7日 下午11:44

http://metformintop.com/ – metformin 1000 mg tablets cost

Richardmab · 2020年9月9日 上午10:33

http://gabapentintop.com/ – neurontin gel

Buy generic cialis · 2020年9月9日 下午9:32

Thanks so much for the post.Really thank you! Keep writing.https://cia20online.com/

Richardmab · 2020年9月10日 下午5:49

http://metformintop.com/ – metformin er 500 mg

AbrahamInhew · 2020年9月12日 下午12:10

plaquenil cost canada – plaquenil

AbrahamInhew · 2020年9月15日 上午1:04

generic plaquenil – plaquenil for sale

Robertmep · 2020年9月15日 下午9:35

otc cialis: buy generic cialis

Buy viagra in hungary · 2020年9月16日 下午7:52

Thanks a lot for the article post.Much thanks again. Fantastic.buy viagra in hungary

Robertmep · 2020年9月17日 上午5:26

lowest price cialis: tadalafil

booospinc · 2020年9月17日 下午5:03

fast custom essays [url=https://customessayjojo.com/]online custom essays[/url] custom my essay custom essay writing sites essay on old custom

htbxelosy · 2020年9月17日 下午5:06

doctoral dissertation writing [url=https://dissertationaas.com/]dissertation chapters[/url] dissertation editors best dissertation writing dissertation writing assistance

RonaldNup · 2020年9月18日 下午2:07

where can i buy zithromax uk: zithromax tablets

RonaldNup · 2020年9月19日 下午9:38

:

bdhkeneta · 2020年9月20日 下午11:07

help on essays [url=http://buyjeacialonline.com/]i need help to write an essay[/url] essay writing help for high school students help with writing essays at university help me write my college essay

RonaldNup · 2020年9月21日 上午4:01

can i buy zithromax over the counter in canada: can you buy zithromax over the counter in canada

DonaldPlate · 2020年9月21日 下午10:50

free cialis medication for providers: buy cialis

DonaldPlate · 2020年9月23日 上午3:11

viagra over the counter: buy viagra online

RonaldSew · 2020年9月24日 下午10:12

generic viagra walmart generic viagra

发表评论

电子邮件地址不会被公开。 必填项已用*标注