JS事件委托:JsAction
JsAction是一個微小的事件委托庫,允許解耦 JavaScript 代碼中處理 action 的 DOM 節點。添加事件處理器傳統的方式就是獲取節點的引用,然后添加到事件處理它。JsAction 允許用戶通過自定義的 HTML 屬性 jsaction 映射事件和這些事件處理器的名字。
用
In the DOM
Actions are indicated with the jsaction attribute. They are separated by ;, where each one takes the form:
[eventType:]<namespace>.<actionName> If an eventType is not specified, JsAction will assume click.
<div id="foo"
jsaction="leftNav.clickAction;dblclick:leftNav.doubleClickAction">
some content here
</div> In JavaScript
Set up
var eventContract = new jsaction.EventContract;
// Register the event types we care about.
eventContract.addEvent('click');
eventContract.addEvent('dblclick');
var dispatcher = new jsaction.Dispatcher;
eventContract.dispatchTo(goog.bind(dispatcher.dispatch, dispatcher)); Register individual handlers
/**
* Do stuff when actions happen.
* @param {!jsaction.ActionFlow} flow Contains the data related to the action
* and more. See actionflow.js.
*
*/
myapp.LeftNav.prototype.doStuff = function(flow) {
// do stuff
};
myapp.LeftNav.prototype.registerHandlers = function() {
dispatcher.registerHandlers(
'leftNav', // the namespace
this, // handler object
{ // action map
'clickAction' : this.doStuff,
'doubleClickAction' : this.doStuff
});
}; 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!