Details
-
Type:
Task
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 6.1.1 CE GA2, 6.1.20 EE GA2
-
Fix Version/s: 6.2.0 CE M4
-
Component/s: UI, UI > JavaScript & CSS
-
Labels:None
-
Similar Issues:
Description
In AlloyUI if you create a function with A.bind or A.rbind to specify it's context or arguments, it will return you a new function like so:
var obj = {
doStuff: function![]()
};
var fn = A.bind(obj.doStuff, obj, 1);
fn(); // prints 1
However, if you try to replace that method on the obj like so:
obj.doStuff = function![]()
If you run:
fn(); // still prints 1, should print 2
The reason is because when it creates the bound function, it's creating it with the function reference, and has no idea that doStuff was replaced.
This causes issues with things like AOP which will displace a method and allow users to call a method before or after it runs, change it's arguments or even it's return value.
The fix is to define it like so:
var fn = A.bind('doStuff', obj, 1);
fn(); // prints 1
obj.doStuff = function
;
fn(); // prints 2
This only works if you're calling the method in the context of the same object that the method is defined on.
So for instance, this doesn't work:
var obj2 = {};
var fn = A.bind('doStuff', obj2);
fn(); // throws error since doStuff doesn't exist on obj2
In that case you must pass a function reference.
