Skip to content

Commit bbe023f

Browse files
committed
Tweak in getRoleMember() method
1 parent b5d4dda commit bbe023f

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

dci/dci-amd.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,63 @@ define(["require", "exports"], function(require, exports) {
22
var isNodeJs = (typeof window == 'undefined' && typeof global != 'undefined');
33
var globalNamespace = isNodeJs ? global : window;
44

5+
if (!isNodeJs) {
6+
if (!Function.prototype.bind) {
7+
Function.prototype.bind = function (oThis) {
8+
if (typeof this !== "function") {
9+
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
10+
}
11+
12+
var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, fNOP = function () {
13+
}, fBound = function () {
14+
return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
15+
};
16+
17+
fNOP.prototype = this.prototype;
18+
fBound.prototype = new fNOP();
19+
20+
return fBound;
21+
};
22+
}
23+
}
24+
525
//TODO Support DestinationAccount['de' + 'posit'](amount)
626
//
727
//DestinationAccount['de' + 'posit'](amount) could be rewritten to:
828
//__context.__$DestinationAccount['de' + 'posit'].call(__context.DestinationAccount, amount)
929
//
1030
//would be better to give a nice error message if role method not found:
11-
//DCI.getRoleMethod(__context, 'DestinationAccount', 'de' + 'posit').call(__context.DestinationAccount, amount)
12-
function getRoleMethod(context, roleName, methodName) {
13-
var roleMethod = context['__$' + roleName][methodName];
14-
if (!roleMethod) {
15-
throw new Error('Method "' + methodName + '" not found on role "' + roleName + '"');
31+
//DCI.getRoleMember(__context, __context.DestinationAccount, 'DestinationAccount', 'de' + 'posit').call(__context.DestinationAccount, amount)
32+
//Gets a member on a role player - can be either a role method or a method or property of the role player object
33+
function getRoleMember(context, player, roleName, memberName) {
34+
if (player != context[roleName]) {
35+
//If we're here, it's because the programmer used `this` inside a closure inside a role method.
36+
//So either `this` refers to some other object besides the current role, or the programmer used `this`
37+
//inside a closure when they should have used `self`.
38+
//
39+
//In other words this code would also be reached if `this` is equal to `undefined`, `global`, or `window`.)
40+
//...for example, if the SourceAccount.transferOut() method in the Transfer Money example contained the following code:
41+
// [1,2,3].forEach(function() {
42+
// this.withdraw(); //`this` is actually equal to `window` or `global` here! (or `undefined` in strict mode)
43+
// });
44+
//
45+
//Because we need to account for the first case (`this` refers to some other object besides the current role),
46+
//which is perfectly valid, we simply return the property on `this` just as would happen normally in Javascript.
47+
return player[memberName];
48+
}
49+
50+
var roleMethod = context['__$' + roleName][memberName];
51+
if (roleMethod) {
52+
//bind the role player as `this` on the specified role method
53+
return roleMethod.bind(player);
54+
} else {
55+
if (!(memberName in player)) {
56+
throw new Error('Method or property "' + memberName + '" not found on role "' + roleName + '" nor on its current role player.');
57+
}
58+
return player[memberName];
1659
}
17-
return roleMethod;
1860
}
19-
exports.getRoleMethod = getRoleMethod;
61+
exports.getRoleMember = getRoleMember;
2062

2163
//This function is for handling calls beginning with `this`; it calls a method on the current role player,
2264
//which could be either a role method or a method on the data object.

dci/dci.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function getRoleMember(context, player, roleName, memberName) {
5151
//bind the role player as `this` on the specified role method
5252
return roleMethod.bind(player);
5353
} else {
54-
if (!player[memberName]) {
54+
if (!(memberName in player)) {
5555
throw new Error('Method or property "' + memberName + '" not found on role "' + roleName + '" nor on its current role player.');
5656
}
5757
return player[memberName];

dci/dci.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export function getRoleMember(context: Object, player: Object, roleName: string,
114114
}
115115
else {
116116
//check for property on role player
117-
if (!player[memberName]) {
117+
if (!(memberName in player)) {
118118
throw new Error('Method or property "' + memberName + '" not found on role "' + roleName + '" nor on its current role player.');
119119
}
120120
return player[memberName];

0 commit comments

Comments
 (0)