• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
XPP的Blog
博客园    首页    新随笔    联系   管理    订阅  订阅

Asp.Net ajax 面向对象类型系统2 - 使用事件

直接上代码了:

//注册命名空间
Type.registerNamespace("MyNamesapce");

MyNamesapce.Eat = function()
{
    //定义EventHandlerList对象
    // this._events = new Sys.EventHandlerList();
    this._events = null;
}
MyNamesapce.Eat.prototype =
    {
        _get_events: function()
        {
            if (!this._events)
            {
                //返回EventHandlerList对象
                this._events = new Sys.EventHandlerList();
            }
            return this._events;
        },

        //添加add和remove eat事件的方法
        add_eatEvent: function(handler)
        {
            this._get_events().addHandler("eat", handler);
        },
        remove_eatEvent: function(handler)
        {
            this._get_events().removeHandler("eat", handler);
        },
        //定义eat事件
        onEat: function(e)
        {
            var eatHandler = this._get_events().getHandler("eat");
            if (eatHandler)
            {
                eatHandler(this, e);
            }
        },
        //定义触发eat事件的方法
        handleEat: function(isHungry)
        {
            if (isHungry)
            {
                this.onEat(Sys.EventArgs.Empty);
            }
        }
    }
MyNamesapce.Eat.registerClass("MyNamesapce.Eat");
//-----------------------------------------------------------
function test()
{
    var eat = new MyNamesapce.Eat();
    //添加eat事件注册
    eat.add_eatEvent(EatEventMethod1);
    eat.add_eatEvent(EatEventMethod2);
    eat.add_eatEvent(EatEventMethod3);

    //触发eat事件
    eat.handleEat(true);
}
//定义eat事件处理方法
function EatEventMethod1(sender, e)
{
    alert("I begin to eat.");
}
function EatEventMethod2(sender, e)
{
    alert("I am eating.");
}
function EatEventMethod3(sender, e)
{
    alert("I've eaten.");
} 


其实这个和C#的EventHandlerList的事件处理方式差不多的,演示地址(查看源文件即是本例):

http://www.xpp.com.cn/AspnetAjax/AjaxOO/MyEvents.aspx

posted @ 2009-12-27 23:40  Peter_xpp  阅读(135)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3