-
Notifications
You must be signed in to change notification settings - Fork 37
/
Extension.ts
62 lines (59 loc) · 2.07 KB
/
Extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
import {ObjectType, scout} from './index';
/**
* This class is used to extend an existing Scout object. In order to use the extension feature
* you must subclass Extension an implement an init method where you register the methods
* you want to extend. Example:
*
* scout.MyExtension.prototype.init = function() {
* this.extend(scout.MyStringField.prototype, '_init');
* this.extend(scout.MyStringField.prototype, '_renderProperties');
* };
*
* Then you implement methods with the same name and signature on the extension class. Example:
*
* scout.MyExtension.prototype._init = function(model) {
* this.next(model);
* this.extended.setProperty('bar', 'foo');
* };
*
* The extension feature sets two properties on the extension instance before the extended method
* is called. Note: the function scope (this) is set to the extension instance when the extended
* function is called:
*
* next: is a reference to the next extended function or the original function of the extended
* object, in case the current extension is the last extension in the extension chain.
*
* extended: is the extended or original object.
*/
export class Extension<E> {
extended: E;
next: (...args: any[]) => any;
extend(extended: E, funcName: string) {
let origFunc = extended[funcName];
let extension = this;
extended[funcName] = function(...args) {
extension.extended = this;
extension.next = origFunc.bind(this);
return extension[funcName](...args);
};
}
/**
* Calls {@link scout.create} for each extension class in the given extensions array.
*
* @param extensions an array of strings containing extension class names
*/
static install(extensions: ObjectType[]) {
extensions.forEach(ext => {
scout.create(ext);
});
}
}