`

YUI3 Infrastructure下的 EventTarget

yui 
阅读更多

 

YUI自定义事件系统允许你使用自定义事件,并且这些事件的可用性比DOM中的事件更高--事件具体到你程序中定义的。用户自定义事件和DOM事件工作非常像。可以冒泡,传递事件等,也可以抑制默认事件行为的传递,及其他。与自定义事件相关的API由EventTarget类提供。其他的基础类继承自EventTarget类,如果你只需要用户自定义事件API,你可以直接扩展或者扩充你自己的类


 

YUI().use('event-custom', function (Y) {
    
});

 


Y.on('anyOldNameYouWant', function () {
    alert("Looky there!");
});

// Group subscriptions by passing an object or array to on()
Y.on({
    somethingImportant: updateCalendar,
    birthday          : eatCake,
    weekendEnd        : backToTheGrindstone
});

// Some events have prefixes
Y.once("fuji:available", climb);

// Custom events have distinct "after" moments
Y.after("spa-category|pedicure", gelatoTime);

Firing Events

// All subscribers to the myapp:ready event will be executed
Y.fire('myapp:ready');

// Pass along relevant data to the callbacks as arguments
Y.fire('birthday', {
    name: 'Walt Disney',
    birthdate: new Date(1901, 11, 5)
});
  
回调参数和facade 事件
/ Simple notification events don't send event objects, only fire() data
Y.on('talkie', function (data) {
    alert('(' + data.time + ') Walkie ' + data.message);
    // data.preventDefault is not defined. data is just a plain object
});

Y.fire('talkie', {
    message: 'roger, over.',
    time: new Date()
});

// Events configured to emitFacade will send an event object, merged with
// fire() data
Y.publish('bill:due', {
    emitFacade: true,
    defaultFn : payUp
});

Y.on('bill:due', function (e) {
    // Event facades have standard properties and methods as well as properties
    // from payload data passed to fire()
    if (e.payee === 'Rich Uncle Sal') {
        e.preventDefault(); // the `payUp` method won't be called (Sal can wait)
    }
});

// Objects passed as the second argument to fire() for facade events will have
// their properties merged onto the facade received by the callback.
Y.fire('bill:due', {
    payee: 'Great Aunt Myra',
    amount: 20
});
使用emitfacade设置为真,就是包裹并保护一个自定义事件需要event-custom-complex模块

继承 EventTarget

function MyClass() {
    /* insert constructor logic here */
}

MyClass.prototype = {
    add: function (item) {
        // You can assume the APIs are available from your class instances
        this.fire("addItem", { item: item });
    },
    ...
};

// Make MyClass an EventTarget
Y.augment(MyClass, Y.EventTarget);

var instance = new MyClass();
instance.on('addItem', function (e) {
    alert("Yay, I'm adding " + e.item);
});

instance.add('a new item'); // ==> "Yay, I'm adding a new item"


添加默认行为

  格式:_def(the name of the event)Fn.

function TreeNode(name) {
    this.name   = name;
    this._items = [];

    // Event publishing is typically done during instantiation
    this.publish('add', {
        defaultFn: this._defAddFn
    });
}

// Adding a child node is an interesting mutation operation. Move the mutation
// logic from the method to a mutation event's default behavior
TreeNode.prototype.add = function (node) {
    this.fire('add', { newNode: node });
};

// Default functions receive the event facade like other subscribers
TreeNode.prototype._defAddFn = function (e) {
    this._items.push(e.newNode);

    e.newNode.addTarget(this);
};

...

branchA.add(leaf1); // without 'add' subscriptions, the behavior is the same



 

分享到:
评论

相关推荐

    yui3-master.zip

    yui3-master.zip

    YUI3 dialog组件

    基于YUI3的dialog组件该组件是基于YUI3开发的,功能强大,详细见http://www.qiqicartoon.com

    YUI 入门教程YUI 入门教程YUI 入门教程

    YUI教程YUI 入门教程YUI 入门教程YUI 入门教程

    从YUI2到YUI3看前端的演变 pdf

    YUI3 引入了粒度更细的模块管理方式,通过异步 HTTP 请求加载模块、然后执行回调来加载和使用模块。现场有很多人提出疑问,大家无非关心的是“效率”二字。个人以为在现阶段,这种方式有一点激进,否能为广大用户所...

    从YUI2到YUI3看前端的演变

    从YUI2到YUI3看前端的演变

    YUI3 完整包

    Yahoo! UI Library (YUI) 是一个开放源代码的 JavaScript 函数库,为了能建立一个高互动的网页,它采用了AJAX, DHTML 和 DOM 等程式码技术。它也包含了许多 CSS 资源。

    yuicompressor-yui compressor

    将yuicompressor-2.4.2.jar 放在c:下,将editor.js放在c:盘下。 将editor.js进行压缩 命令为: C:\java -jar yuicompressor-2.4.2.jar editor.js -o editor2.js 参数说明: yuicompressor-2.4.2.jar 为工具包...

    yui3-3.17.2最新版

    yui对于开发者来说是绝对的好用,开发者福利,特献上最新版

    yui_2.9.0前端UI

    YUI 库,全称Yahoo! UI Library。是一组工具和控件,用JavaScript写成, 为的是用DOM 脚本,DHTML和AJAX等技术创建丰富的网页交互式应用程序。 YUI 基于BSD协议,对所有的使用方式都是免费的。YUI 项目包括YUI 库和两...

    yui 3.1.2 源码 6MB大小 0资源分

    YUI3 源码 非compress版 YUI3 源码 非compress版 YUI3 源码 非compress版

    高效WEB前端开发之路:YUI3.15

     本书作者便是在此背景下,以国外最优秀的JavaScript框架之一——Yahoo User Interface Library(简称YUI)的最新版本YUI 3.15为基础编写而成。本书通过通俗易懂的语言和大量丰富的实例,帮助读者解决实际生产环境...

    YUI3 中tree的两种实现

    NULL 博文链接:https://ttwang.iteye.com/blog/1741631

    YUI3.6文档及示例

    yui官网下载的。内容很全,示例+doc说明

    js 压缩YUI

    雅虎的东西,简单的操作很好用 使用例子:java -jar D:\yuicompressor\yuicompressor\yuicompressor.jar E:\js\all.js -o E:\wap\wap2\js\all-min.js --charset utf-8 当然要装jdk了 不然就玩完了

    yuitest YUI测试工具

    YUI Test is a complete testing framework for JavaScript and Web applications. You can use the simple JavaScript syntax to write unit tests that can be run in web browsers or on the command line, as ...

    yui_2.6.0r2

    yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2

    yui 资源包

    yui 源码下载,3.9.0 r2 包,最新版本

    yuicompressor,给YUI Compressor添加右键命令

    YUI Compressor非常好用,特别是JS的混淆是众多JS Coding的最爱。可惜官网提供的版本都不具备右键功能,每次压缩都要cmd输入一些命令实在是繁琐,本文就介绍如何给YUI Compressor添加右键命令,方便使用。 网上已有...

    YUI3 Cookbook

    YUI3 Cookbook for Yahoo User Interface 3

Global site tag (gtag.js) - Google Analytics