Ticket #880 (closed bug: fixed)

Opened 6 years ago

Last modified 3 years ago

Event object conflict

Reported by: euzuro Owned by: crschmidt
Priority: major Milestone: 2.5 Release
Component: Events Version: 2.4
Keywords: Cc:
State:

Description

As reported by Paul S to dev@

 As part of removing prototype.js
dependency, it appears that you have kept most of Prototype's event
handling code, including the following (lines 327-331 of Events.js):

if (window.Event) {
  OpenLayers.Util.extend(window.Event, OpenLayers.Event);
} else {
  var Event = OpenLayers.Event;
}

If you include OpenLayers.js AFTER including prototype.js, this
effectively replaces most of prototype's Event object with the
OpenLayers.Event object, which has undesirable results (i.e. calling
Event.observe is actually calling OpenLayers.Event.observe)

Is there a particular reason why we are putting OpenLayers.Event into
the global namespace?

Attachments

event.patch Download (0.6 KB) - added by crschmidt 6 years ago.
event_stop.patch Download (438 bytes) - added by crschmidt 6 years ago.
events.patch Download (458 bytes) - added by crschmidt 6 years ago.
events.2.patch Download (1.2 KB) - added by euzuro 6 years ago.
take 5! here it is, everyone safe and sound
events.3.patch Download (1.2 KB) - added by euzuro 6 years ago.
TAKE 6!! SAFER AND SOUNDER!!!

Change History

Changed 6 years ago by crschmidt

  • owner changed from euzuro to crschmidt

Taking this one, because it shouldn't be done anymore. I'll get a patch up for this soon.

Changed 6 years ago by crschmidt

  • status changed from new to assigned

Changed 6 years ago by crschmidt

The reason for originally having Event in the main namespace is that we had encouraged people (through 2.0, I think) to use Event.stop in their client code. We now only use OpenLayers.Event internally, but we wanted to ensure that users who had used 'Event.stop' would not have their applications break.

Looking at the prototype code, it seems like Prototype will extend any Event object it finds. This means that we should, I think, be able to change:

if (window.Event) {

OpenLayers.Util.extend(window.Event, OpenLayers.Event);

} else {

var Event = OpenLayers.Event;

}

to just:

if (!window.Event) {

var Event = OpenLayers.Event;

}

That way, users who are not using Prototype and are using Event.stop will get their stop still.

Then, in 3.0, we remove it.

Changed 6 years ago by crschmidt

Changed 6 years ago by crschmidt

  • keywords review added

Comments welcome.

Changed 6 years ago by tschaub

  • keywords commit added; review removed

If we can't get rid of Event entirely, I think this makes sense.

Please commit.

Changed 6 years ago by crschmidt

  • keywords commit removed
  • status changed from assigned to closed
  • resolution set to fixed

(In [4040]) "Event object conflict: If you include OpenLayers.js AFTER including prototype.js, this effectively replaces most of prototype's Event object with the OpenLayers.Event object, which has undesirable results (i.e. calling Event.observe is actually calling OpenLayers.Event.observe)." Fixed by only creating Event is Event does not already exist. (Closes #880)

Changed 6 years ago by euzuro

(In [4246]) use the namespace corrected event.stop(). (See #880)

Changed 6 years ago by euzuro

  • status changed from closed to reopened
  • resolution fixed deleted

as cr5 and i just discovered. um... this isnt working correctly

Changed 6 years ago by crschmidt

The problem is that the window.Event variable exists. We only care about keeping one function from Event, so instead we'll just check for that -- it's the only one we ever used in our examples/API as a suggestion for users back in the prototype days. Patch incoming.

Changed 6 years ago by crschmidt

Changed 6 years ago by crschmidt

  • keywords review added

Hard to test, since we'd have to muck about before the OpenLayers stuff is loaded. Can put one together if it's important.

Changed 6 years ago by crschmidt

Changed 6 years ago by crschmidt

so erik pointed out that only applying stop, was totally arbitrary

*and*

we have applyDefaults

which does what we actually want.

So, new patch does that

Changed 6 years ago by euzuro

take 5! here it is, everyone safe and sound

Changed 6 years ago by euzuro

TAKE 6!! SAFER AND SOUNDER!!!

Changed 6 years ago by crschmidt

  • keywords commit added; review removed

heh, heh

Erik and I investigated a little more and found out htat IE has no window.Event like FF. so this patch is what we should have done a while back, but I forgot about applyDefaults.

Go! Commit! Fix!

Changed 6 years ago by euzuro

  • keywords commit removed
  • status changed from reopened to closed
  • resolution set to fixed

(In [4271]) Here we have finally solved the smashing of the Event object problem. Once and forall. God save the queen when 3.0 comes we're getting rid of this sloppiness. See r4040 for more info on why we've done all this. (Closes #880)

Changed 3 years ago by gingerbbm

This code is causing problems in the specific case of inluding OpenLayers.js after Prototype.js in Internet Explorer versions 6, 7 and 8.

    // FIXME: Remove this in 3.0. In 3.0, Event.stop will no longer be provided
    // by OpenLayers.
    if (window.Event) {
        OpenLayers.Util.applyDefaults(window.Event, OpenLayers.Event);
    } else {
        var Event = OpenLayers.Event;
    }

I do not know why window.Event is undefined in IE. I put together the following test that loads each library script in the requisite order, testing for window.Event and Event.pointerX before each load:

 http://gingerbbm.com/dev/conflict/conflicttest.html

window.Event appears to be available throughout, but in Events3.js it suddenly isn't. (I split Events.js into four separate files when homing in on the problem and added one line to Events3.js to check for the prescence of window.Event.) This results in window.Event being overwritten and consequently methods like Event.pointerX() become unavailable.

As the comment in the code says, "Remove this in 3.0". In cursory checks removing this code at 2.8 appears to be OK but being unfamiliar with a lot of the features of OL I don't know how risky this is.

I initially posted this information to the mailing list at  http://n2.nabble.com/OpenLayers-and-Prototype-conflict-in-IE-td4591037.html#a4591037.

I'm using OpenLayers 2.8.

Changed 3 years ago by gingerbbm

Apologies. I've now opened as a new bug under  http://trac.openlayers.org/ticket/2495.

Note: See TracTickets for help on using tickets.