`
yiminghe
  • 浏览: 1431523 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

unified event model

 
阅读更多

为了处理原生事件在各个浏览器下的兼容性,上次提出了应对原生事件的事件模型,但随着web的多样性以及新功能需求,常常需要自行实现 ui 组件,例如 按钮菜单等,实现时推荐尽量和原生组件(select , input 等)保持一致,那么也需要暴露事件,不过这时是自定义事件,如果想把模拟 ui 组件做到和原生控件完全一致的体验,那么自定义事件也要具备和原生事件一样的能力。

 

自定义事件

 

自定义事件其实是 observer-listener 模式的通俗叫法,本质上和浏览器的事件模型是一致的,由于之前为了应对兼容性问题在客户端已经重新实现了一遍原生事件模型,那么就可以把自定义事件也集成到这个模型中,形成统一的事件模型。

 

区别点

 

自定义事件由于完全由应用控制,那么就和原生事件有一些区别:

 

1. 不需要关联到 dom 节点

 

     虽然原生事件可以由自己完全实现一套,但是最终仍然要调用 addEventListener/attachEvent 对dom节点进行关联(接受浏览器事件通知),而自定义事件则完全避免了这一步。

 

2. 需要添加冒泡 api

 

    原生事件冒泡不需要模型自己控制,浏览器自会处理,模型只要提供接口操作浏览器 api 控制冒泡行为即可(stopPropagation)。而自定义事件由模型完全控制,那么模型就需要提供冒泡 api,并且可以指定该事件是否可以冒泡以及事件触发源对象的上一级冒泡对象,当触发自定义事件时,模型需要根据冒泡链依次向上触发,并根据是否调用了 stopPropagation 来停止事件传播。

 

3. 不需要默认事件阻止 api

 

    一般来说原生事件都会附带一些默认行为(a 页面跳转,submit 表单提交等),模型也会提供相应的阻止 api (preventDefault)。而自定义事件则完全不用考虑这个了。

 

4. 事件的程序触发方式简单(programmatic trigger)

 

    自定义事件的触发比较简单,只要注意自行冒泡即可。而原生事件的程序触发则比较麻烦,虽然可以通过构建原生事件对象交给浏览器dispatch,可这种方式兼容性问题很大(change 等事件根本没法触发),最好的方式还是由模型自己触发,这时需要考虑的是冒泡以及默认事件行为触发。

 

 

区分

 

以上的区别点模型需要在适当的时候进行分支处理,而判断关键就在于当前对象是否是原生对象(是否具有原生事件绑定接口):

 

 

obj.addEventListener||obj.attachEvent
 

最终 api

 

最终模型暴露出来的 api 应该是:

 

obj.on(name,fn,context)

obj.detach(name,fn,context)

obj.addBubbleTarget(otherObj) // 自定义事件专属

obj.configBubble(name,isBubble); // 自定义事件专属

obj.fire(name,data)
 
分享到:
评论

相关推荐

    netWindows_0.3.0_pre2

    an advanced, unified event model (signals and slots) * an extensible component framework * automatic environment setup (include a single file and go). The widgets provided with ...

    Professional WPF Programming

    subsystems offered in WPF, such as the dependency property system and the routed event model. You learn how to develop and deploy WPF applications targeting both the desktop and the browser. This book...

    javaee API

    javax.el Provides the API for the Unified Expression Language shared by the JSP 2.1 and JSF 1.2 technologies. javax.enterprise.deploy.model Provides Tool Vendor implementation classes. javax....

    Software.Testing.A.Craftsmans.Approach.4th.Edition.1466560681

    Introduces an innovative approach that merges the Event-Driven Petri Nets from the earlier editions with the "Swim Lane" concept from the Unified Modeling Language (UML) that permits model-based ...

    用Rhapsody和UML开发导航控制

    This project has the objective of familiarising with Rhapsody, a Unified Modeling Language (UML) based development tool. The additional goal of embedding the developed software in a TMS-320LF2407 ...

    Microsoft Press-Programming Microsoft Outlook And Exchange 2003 (2 of 2)

    The Unified Object Model 14 The PropertyAccessor 14 Stores and Storage Items 24 User Interface Objects 26 Accounts 28 Performance Improvements 30 Working with Tables Instead of Items 30 Filtering ...

    Microsoft Press-Programming Microsoft Outlook And Exchange 2003 (1 of 2)

    The Unified Object Model 14 The PropertyAccessor 14 Stores and Storage Items 24 User Interface Objects 26 Accounts 28 Performance Improvements 30 Working with Tables Instead of Items 30 Filtering ...

    Beyond.BIOS.Developing.with.the.UEFI.3rd.epub

    This book provides an overview of modern boot firmware, including the Unified Extensible Firmware Interface (Uefi) and its associated Efi Developer Kit Ii (Edkii) firmware. The authors have each made ...

    React and React Native [Kindle Edition]

    Use the Flexbox layout model to create responsive mobile designs Leverage the native APIs of Android and iOS to build engaging applications with React Native Respond to gestures in a way that's ...

    Fuzzy Control Systems

    Chapter 7—Intelligent Fuzzy Controller for Event-Driven, Real Time Systems and its VLSI Implementation 1 Fuzzy Logic Finite State Machine 2 Algorithm of Creating a Multiple-Input Fuzzy Model 3...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    The Unified Type System 109 Reference Type and Value Type Memory Allocation 114 Reference Type and Value Type Assignment 117 More Differences Between Reference Types and Value Types 121 C# ...

    JAVA核心技术

    JSP(TagLib)——>ActionForm——>Action ——> Event——>EJBAction——>EJB ——>DAO——>Database JSP(TagLib) (forward) <——Action <——EventResponse<—— ??Turbine:??主要应用方面:WEB层。??主要应用...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 ...PEP 341: Unified try/except/finally PEP 342: New Generator Features PEP 343: The ‘with...

    asp.net知识库

    在ASP.NET页面中推荐使用覆写(Override)而不是事件处理(Event Handler) 常用编码工具类,支持base64,md5,des,crc32 也谈谈技术面试 在C#里把ArrayList转换为Array 或 把Array转换为ArrayList C# 2.0 在.NET 2.0中...

    JavaEE 5.0 Tutorial.pdf

    Java EE ApplicationModel ................................................................................................................. 42 DistributedMultitiered Applications .........................

Global site tag (gtag.js) - Google Analytics