prestonparris prestonparris - 1 year ago 56
Javascript Question

problems creating custom event dispatcher in google closure library

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download