problems creating custom event dispatcher in google closure library

笑着哭i 提交于 2019-12-07 12:01:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5411840/problems-creating-custom-event-dispatcher-in-google-closure-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!