This project is archived and is in readonly mode.

#1199 ✓wontfix
André Cruz

Class Events enchancement

Reported by André Cruz | March 18th, 2011 @ 02:25 PM

Hi.

I'm currently working in a project in wich I needed to stop the propagation of a class event that uses the Events class/mixin.

I ended up implementing the functionality myself. Is there any change that you guys implement this or similar functionliaty in the official release?

The fireEvent function was modified into:

    fireEvent: function(type, args, delay){
            type = removeOn(type);
            var events = this.$events[type];
            if (!events) return this;
            args = Array.from(args);

            var dispatch = function() {
                var nrEvents = events.length;
                for (var x = 0; x < nrEvents; x++) {
                    var ret = events[x].apply(this, args);
                    if (typeOf(ret) == "boolean" && !ret) break;
                }
            }

            if (delay) dispatch.delay(delay, this, args);
            else dispatch.apply(this, args);

            return this;
    },

This way a listener could return false and the events would not be propagated to the remaining listeners.


André Cruz

Comments and changes to this ticket

  • André Cruz

    André Cruz March 18th, 2011 @ 02:27 PM

        fireEvent: function(type, args, delay){
                type = removeOn(type);
                var events = this.$events[type];
                if (!events) return this;
                args = Array.from(args);
    
                var dispatch = function() {
                    var nrEvents = events.length;
                    for (var x = 0; x < nrEvents; x++) {
                        var ret = events[x].apply(this, args);
                        if (typeOf(ret) == "boolean" && !ret) break;
                    }
                }
    
                if (delay) dispatch.delay(delay, this, args);
                else dispatch.apply(this, args);
    
                return this;
        },
    

    Didn't noticed that we could format source code.

  • André Cruz

    André Cruz March 18th, 2011 @ 02:32 PM

    requestInstance.addEvent("success", function(response) {
    
        if (/* response parsing detects errors */) {
             this.fireEvent("failure");
             return false;
        }
        
        /* other code ... */
    });
    

    The example above ilustrastes how it could be used.

  • André Cruz
  • Christoph Pojer

    Christoph Pojer March 24th, 2011 @ 09:30 AM

    • State changed from “new” to “wontfix”

    Hello,

    please have a look at: http://blog.calyptus.eu/seb/2009/07/why-you-shouldnt-return-false-i...

    There are a few things which are true for Class Events too, for example "Returning a false value is a relic from the old days when we only had a single listener per event." and "Second of all, the “return false” API doesn’t make sense. The function isn’t failing. It isn’t canceled. In fact, it’s canceling a DIFFERENT function."

    In addition please note that you must not rely on the order of events. This means that if you want to stop all other events within an event, it may very well be that all the other events have already executed before.

  • André Cruz

    André Cruz April 2nd, 2011 @ 06:34 PM

    Well returning false might be a bad idea, but at least we should have a way to stop the event (e.g. passing an event object with functionality to stop it?)

    Let me disagree about the order of events. There are cases in which a function can return a class that implement the Events mixin that already have observers bound. Those observers, according to specific conditions, should be able to prevent other observers to be fired.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket

Pages