在谷歌闭包库中创建自定义事件调度程序时出现问题

发布于 2024-10-25 04:45:46 字数 1279 浏览 5 评论 0原文

我正在尝试在 google Closure js 库中创建自定义事件调度程序。我将此代码基于 fx 文件夹中的动画类,但我不断收到此错误..

“goog.events 未定义”

但我将事件包包含在顶部。这是我的代码。

    goog.provide('test.util.Animation');
    goog.provide('test.util.Animation.EventType');
    goog.provide('test.util.AnimationEvent');

    goog.require('goog.events');
    goog.require('goog.events.EventTarget');
    goog.require('goog.events.EventType');



    /**
    * Constructor for an animation object.
    * @constructor
    * @extends {goog.events.EventTarget}
    */
    test.util.Animation = function() {
      goog.events.EventTarget.call(this);
    };
    goog.inherits(test.util.Animation, goog.events.EventTarget);

    /**
    * Events fired by the animation.
    * @enum {string}
    */
    test.util.Animation.EventType = {
       ANIM_IN: 'anim_in',
       ANIM_OUT: 'anim_out'
    };

    /**
    * Class for an animation event object.
    * @extends {goog.events.Event}
    */
    test.util.AnimationEvent = function(type, anim) {
       goog.events.Event.call(this, type);
    };
    goog.inherits(test.util.AnimationEvent, goog.events.Event);

我包含了所有必要的文件以及我编写的其他代码中的所有其他内容,运行良好。只是当我尝试从 goog.events.EventTarget 继承时,它会抛出此错误。我需要包含一些内容才能继承吗? 如果我删除继承调用,那么它不会抛出错误,但这违背了我想要做的事情的目的。有什么想法吗?谢谢。

I'm trying to create a custom event dispatcher in google closure js library. I'm basing this code off of the animation class in the fx folder, yet I keep getting this error..

"goog.events is undefined"

yet I'm including the events package at the top. here is my code.

    goog.provide('test.util.Animation');
    goog.provide('test.util.Animation.EventType');
    goog.provide('test.util.AnimationEvent');

    goog.require('goog.events');
    goog.require('goog.events.EventTarget');
    goog.require('goog.events.EventType');



    /**
    * Constructor for an animation object.
    * @constructor
    * @extends {goog.events.EventTarget}
    */
    test.util.Animation = function() {
      goog.events.EventTarget.call(this);
    };
    goog.inherits(test.util.Animation, goog.events.EventTarget);

    /**
    * Events fired by the animation.
    * @enum {string}
    */
    test.util.Animation.EventType = {
       ANIM_IN: 'anim_in',
       ANIM_OUT: 'anim_out'
    };

    /**
    * Class for an animation event object.
    * @extends {goog.events.Event}
    */
    test.util.AnimationEvent = function(type, anim) {
       goog.events.Event.call(this, type);
    };
    goog.inherits(test.util.AnimationEvent, goog.events.Event);

I am including all of the necessary files and everything else in the other code I have written runs fine. Its just when I try to inherit from goog.events.EventTarget that it throws this error. Is there something I need to include in order to inherit?
If I remove the inherits call then It will not throw the error, but that defeats the purpose of what I'm trying to do. any ideas? thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-11-01 04:45:47

我在谷歌闭包库讨论组中收到了对此的回答。这是解决方案。

在导入脚本之前放置事件 require:

<script>goog.require('goog.events');</script>
<script src="whatever your script is.js"></script>

问题是 goog.require() 需要在比您使用代码更早的通道上进行评估,并且 goog.inherits() 在同一通道上运行。

I received an answer to this in the google closure library discussion group. Here is the solution.

Put the events require before you import the script:

<script>goog.require('goog.events');</script>
<script src="whatever your script is.js"></script>

The problem is that goog.require() needs to be evaluated on an earlier pass than your use of the code, and goog.inherits() is run on the same pass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文