@@ -81,7 +81,12 @@ this.createjs = this.createjs||{};
8181 * console.log(instance == this); // true, "on" uses dispatcher scope by default.
8282 * });
8383 *
84- * If you want to use addEventListener instead, you may want to use function.bind() or a similar proxy to manage scope.
84+ * If you want to use addEventListener instead, you may want to use function.bind() or a similar proxy to manage
85+ * scope.
86+ *
87+ * <b>Browser support</b>
88+ * The event model in CreateJS can be used separately from the suite in any project, however the inheritance model
89+ * requires modern browsers (IE9+).
8590 *
8691 *
8792 * @class EventDispatcher
@@ -184,7 +189,11 @@ this.createjs = this.createjs||{};
184189 * only run once, associate arbitrary data with the listener, and remove the listener.
185190 *
186191 * This method works by creating an anonymous wrapper function and subscribing it with addEventListener.
187- * The created anonymous function is returned for use with .removeEventListener (or .off).
192+ * The wrapper function is returned for use with `removeEventListener` (or `off`).
193+ *
194+ * <b>IMPORTANT:</b> To remove a listener added with `on`, you must pass in the returned wrapper function as the listener, or use
195+ * {{#crossLink "Event/remove"}}{{/crossLink}}. Likewise, each time you call `on` a NEW wrapper function is subscribed, so multiple calls
196+ * to `on` with the same params will create multiple listeners.
188197 *
189198 * <h4>Example</h4>
190199 *
@@ -254,6 +263,9 @@ this.createjs = this.createjs||{};
254263 /**
255264 * A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the
256265 * .on method.
266+ *
267+ * <b>IMPORTANT:</b> To remove a listener added with `on`, you must pass in the returned wrapper function as the listener. See
268+ * {{#crossLink "EventDispatcher/on"}}{{/crossLink}} for an example.
257269 *
258270 * @method off
259271 * @param {String } type The string type of the event.
@@ -299,19 +311,24 @@ this.createjs = this.createjs||{};
299311 * @method dispatchEvent
300312 * @param {Object | String | Event } eventObj An object with a "type" property, or a string type.
301313 * While a generic object will work, it is recommended to use a CreateJS Event instance. If a string is used,
302- * dispatchEvent will construct an Event instance with the specified type.
303- * @return {Boolean } Returns the value of eventObj.defaultPrevented.
314+ * dispatchEvent will construct an Event instance if necessary with the specified type. This latter approach can
315+ * be used to avoid event object instantiation for non-bubbling events that may not have any listeners.
316+ * @param {Boolean } [bubbles] Specifies the `bubbles` value when a string was passed to eventObj.
317+ * @param {Boolean } [cancelable] Specifies the `cancelable` value when a string was passed to eventObj.
318+ * @return {Boolean } Returns false if `preventDefault()` was called on a cancelable event, true otherwise.
304319 **/
305- p . dispatchEvent = function ( eventObj ) {
320+ p . dispatchEvent = function ( eventObj , bubbles , cancelable ) {
306321 if ( typeof eventObj == "string" ) {
307- // won't bubble, so skip everything if there's no listeners:
322+ // skip everything if there's no listeners and it doesn't bubble :
308323 var listeners = this . _listeners ;
309- if ( ! listeners || ! listeners [ eventObj ] ) { return false ; }
310- eventObj = new createjs . Event ( eventObj ) ;
324+ if ( ! bubbles && ( ! listeners || ! listeners [ eventObj ] ) ) { return true ; }
325+ eventObj = new createjs . Event ( eventObj , bubbles , cancelable ) ;
311326 } else if ( eventObj . target && eventObj . clone ) {
312327 // redispatching an active event object, so clone it:
313328 eventObj = eventObj . clone ( ) ;
314329 }
330+
331+ // TODO: it would be nice to eliminate this. Maybe in favour of evtObj instanceof Event? Or !!evtObj.createEvent
315332 try { eventObj . target = this ; } catch ( e ) { } // try/catch allows redispatching of native events
316333
317334 if ( ! eventObj . bubbles || ! this . parent ) {
@@ -330,7 +347,7 @@ this.createjs = this.createjs||{};
330347 list [ i ] . _dispatchEvent ( eventObj , 3 ) ;
331348 }
332349 }
333- return eventObj . defaultPrevented ;
350+ return ! eventObj . defaultPrevented ;
334351 } ;
335352
336353 /**
0 commit comments