API: decorators
Source:
component/decorators.ts
API DI (Dependency Injection) Decorators
- Provides a hybrid implementation of decorators for Dependency Injection (DI) that supports both legacy JavaScript environments (like Mozilla Rhino or older GraalJS) using the
(target, propertyKey)signature, and modern JavaScript environments using the decorator metadata andcontext.addInitializer.
Usage
javascript
// -------------------
// PaymentComponent.ts
// -------------------
import { Component } from "sdk/component";
@Component('myPaymentService')
export class PaymentComponent {
public doPayment(paymentData: any): any {
return { status: "OK", data: paymentData };
}
}
// Ensure the class is the final expression so GraalJS returns it.
PaymentComponent;
// -----------------
// OrderProcessor.ts
// -----------------
import { response } from "sdk/http";
import { Injected, Inject } from "sdk/component";
import { PaymentComponent } from "./PaymentComponent"
@Injected()
class OrderProcessor {
@Inject('myPaymentService')
paymentComponent!: PaymentComponent;
}
let orderProcessor = new OrderProcessor();
if (!orderProcessor.paymentComponent) {
response.println("orderProcessor is undefined ");
} else {
response.println("Do Payment: " + JSON.stringify(orderProcessor.paymentComponent.doPayment("123.45")));
}
// -------------------------------------------------------------------------------
// https://github.com/dirigiblelabs/sample-component-decorators
// -------------------------------------------------------------------------------