This is a migrated thread and some comments may be shown as answers.

Bug in kendo.Observable?

3 Answers 42 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
David Rainton
Top achievements
Rank 1
David Rainton asked on 17 Feb 2014, 10:15 PM
It looks like method "one" (or "bind" with last argument true) in kendo.Observable doesn't work as expected if you try to bind several events at the same time.
Short example is at:
http://jsfiddle.net/737D2/

Expected result is 4 alerts in following order:
- "b"
- "Before a"
- "a"
- "After a"

Actual result is:
- "b"
- "Before a"
- "b"
- "b"
- "After a"

Is this a real bug or is it intended behavior?


In the source code I'm looking at now (Kendo UI Web v2013.3.1119), the bug is at \src\js\kendo.core.js in lines 90-126 where the method "bind" of the class
"Observable" is defined. More specifically, most probably lines 113-119 have to be patched. The bug is related to "closures". 
Relevant code is:

            for (idx = 0, length = eventNames.length; idx < length; idx++) {
                eventName = eventNames[idx];

                handler = handlersIsFunction ? handlers : handlers[eventName];

                if (handler) {
                    if (one) {
                        original = handler;
                        handler = function () {
                            that.unbind(eventName, handler);
                            original.apply(that, arguments);
                        };
                    }

                    if (one) {
                        handler = (function (original, localEventName) {
                            var newHandler = function () {
                                that.unbind(localEventName, newHandler);
                                original.apply(that, arguments);
                            };
                            return newHandler;
                        })(handler, eventName);
                    }

                    events = that._events[eventName] = that._events[eventName] || [];
                    events.push(handler);
                }
            }


I think proper fix should be something like this (I didn't test it):

     if (one) {
         handler = (function (original, localEventName) {
             var newHandler = function () {
                 that.unbind(localEventName, newHandler);
                 original.apply(that, arguments);
             };
             return newHandler;
         })(handler, eventName);
     }

Note that in this case "original", "localEventName" and "newHandler" are captured from the local scope and thus will not be affected by further iteration through for cycle.  



3 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 19 Feb 2014, 09:03 AM
Hello David,

I'm afraid that the way you are tying to use the bind method is not supported.

If you want to bind multiple event handlers at once you will need to pass single key/value object. Where the key is the event name and the value is the event handler:

myObservable.bind({
"event1" : function() { alert("handle event 1"); }, 
        "event2" : function() { alert("handle event 2"); }
});

Also when binding to a multiple events at once, the one option is not supported as well as the one method supports only binding to a single event.

Regards,
Rosen
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
David Rainton
Top achievements
Rank 1
answered on 19 Feb 2014, 05:39 PM
I definitely can see Kendo using similar syntax in its own code (in many widgets) and of course I'd like to use it as well. Are there any fundamental reasons why bind can't be implemented to reliably support binding multiple events/handlers? Particularly is there something wrong with my suggested patch?
0
Rosen
Telerik team
answered on 20 Feb 2014, 06:54 AM
Hi David,

Could you please point me where you have found bind method to be used in such way and passing true as 3rd argument. Indeed, you are free to use it with array and an options object, however in this case the "one" is not supported.

Regards,
Rosen
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
David Rainton
Top achievements
Rank 1
Answers by
Rosen
Telerik team
David Rainton
Top achievements
Rank 1
Share this question
or