Comments (4)
很高兴,其实我一直也会这么写,并且会为actionList里的每一个属性(action)重新指定上下文及event;
类似于:
actionList[actionName] && actionList[actionName].call(this,event);
好处是:
我们会有一些场景需要操作触发该action的元素;
坏处是:
trigger的时候,[context]就会出错,而且代码阅读起来会比较混乱;
请问有没有更好的方式解决这个问题?比如trigger的时候自动绑定正确的上下文?
from action.
@litson 谢谢留言。
trigger的时候,[context]就会出错,而且代码阅读起来会比较混乱;
请问有没有更好的方式解决这个问题?比如trigger的时候自动绑定正确的上下文?
Action 目前的设计可以参考这里(#19),最后一节讨论了 .trigger()
方法的使用问题。
from action.
个人觉得Backbone里面的event map也比较方便
events: {
'click [data-action="my-action"]': 'doMyAction'
}
而且上下文是自动绑定
from action.
@anhulife 谢谢评论。
Backbone 的这种机制自然是极好的,但不是每个项目都会使用或适合使用 Backbone。Action 专注做这一件事,而且它要解决的问题十分通用,使得它适用于大多数项目。而且 Action 的体积十分小巧,minify + gzip 后只有 500 多字节,对任何项目来说几乎都是零负担。
关于 “上下文自动绑定”,Action 也是这样设计的。参见 #19。
from action.
Related Issues (17)
- Summary
- 简介
- 建议不要用issue做文档 HOT 7
- 在移动浏览器当中, 使用事件委托,绑定在 body 元素上,有的时候会冒泡不上去 HOT 2
- 动作函数的执行上下文(`this` 指向)是如何处理的? HOT 1
- 需要为 `.add()` 方法增加键名的特殊字符过滤 HOT 1
- 所有点击都要冒泡到 body 元素再处理,性能如何?
- 为什么 `.add()` API 没有采用队列机制? HOT 1
- 所有元素都可以用 Action 来绑定点击事件吗? HOT 1
- 请问 action 是如何处理 300ms 延迟 && 点透问题的呢? HOT 3
- 添加 UMD 包 HOT 1
- 关于 DEBUG_INFO 的疑惑? HOT 2
- 为啥事件触发时回调里不返回event对象呢? HOT 8
- 为什么不弄成通用的呢? HOT 3
- 埋点的核心问题,数据传递怎么搞 HOT 2
- API Documentation / API 文档 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from action.