Make the Wrapper object listen to the GM object and retrigger all events on the Wrapper. For example:
function Shape() {
this.shape_ = new google.maps.Polygon(...);
var self = this;
google.maps.event.addListener(
this.shape_, 'click', function(e) {
google.maps.event.trigger(self, 'click', e);
});
...
}
- Ben
Android brevity.
1 - You're subclassing/extending GM objects. We don't recommend that
precisely to avoid property collisions. Instead we recommend
composing objects: for example a Shape object could contain a
google.maps.Polygon object rather than extending google.maps.Polygon.
2 - You're extending the JavaScript Object or Array prototypes. We
don't recommend you do that, to avoid property collisions.
3 - You're setting properties on GM objects. In that case you want to
avoid obfuscation. For example:
--- Bad ---
var polygon = new google.maps.Polygon(...);
polygon.myId = 5; // For later identification in an event handler
------
--- Good ---
var polygon = new google.maps.Polygon(...);
polygon['myId'] = 5; // For later identification in an event handler
------
In the "bad" code, .myId may be obfuscated to collide with an
obfuscated property of google.maps.Polygon. In the "good" code, the
Closure Compiler will not obfuscate 'myId', so it will not
accidentally collide with obfuscated API properties.
- Ben
Great pointers, thanks a bunch!Michael Bolin mentions that in his book (which I strongly recommend by the way, if anyone else is interested). Apparently, the Closure Compiler will consider a literal property name (declared with an array-like notation) as something the developer doesn't want to have optimized/obfuscated.
There are indeed cases in which I need to append properties to GM objects, particularly when __gm_id doesn't cut it or when I need to create in the GM object an awareness of the soundings (the wrapper for instance), soshape = new CustomShape( /* constructor provides a CustomShape.id*/ );shape.poly = new google.maps.Polygon(...);shape.poly['parentId'] = shape.id;or something in this line. Of course the second and third lines happen within the constructor, but I'm lazy to fix that now.Do you think using the __gm_id property has any serious disadvantages other than not having the guarantee that it'll the same the next time 'round, when I F5 the app?
Also, what's the deal with the messed up events? Why are some events simple objects with just lat/long, and others are more complex dom events, with a long property chain and stuff like event.responseValue, yadda yadda? Why aren't all events triggered in the map following the same pattern?
Should I move this question to a different (perhaps a new) post?
Thank you!!!