Skip to content

Commit ae299c0

Browse files
committed
added DCI.ts
1 parent 1d3a084 commit ae299c0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

samples/dci/DCI.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export class Context
2+
{
3+
//For plain JS version
4+
//Returns a constructor function to instantiate a new context.
5+
//In the future it may also inherit some utility methods that are common to all contexts.
6+
//
7+
//It will check whether the passed callback function has a bindRoles() method, and if so,
8+
//any constructor arguments will be passed to bindRoles(). If the passed callback does not
9+
//have a bindRoles() method, then the roles should be bound at the top of the callback function.
10+
//
11+
//bindRoles() is useful for the case where you want to be able to re-bind the roles on an existing
12+
//context object.
13+
//
14+
static extend(callback): any {
15+
return function(...args : any[]) {
16+
//We always pass the parameters to the passed function (callback) because
17+
//we can't check whether or not a bindRoles() method exists until after we've
18+
//instantiated the context object.
19+
//The ContextConstructor function is needed so that we can pass constructor arguments dynamically
20+
//(see http://stackoverflow.com/a/13931627/560114)
21+
var ContextConstructor: any = function() {
22+
return callback.apply(this, args);
23+
}
24+
ContextConstructor.prototype = callback.prototype;
25+
26+
var context = new ContextConstructor();
27+
context.constructor = callback;
28+
29+
if (context.bindRoles) context.bindRoles.apply(context, arguments);
30+
return context;
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)