The library exposes an isEmpty() method. However, when I add my custom element into a dialog (to allow a user to sign their name in the dialog), I want to be able to check if a signature has been entered or not. I wanted to do this by calling the isEmpty() method, but that is embedded in the custom element so its not (AFAIK) available to the dialog.
Is there a way to expose this method through my custom element so that I can call it from my dialog?
signature-input.ts
import SignaturePad from 'signature_pad'
export class SignatureInputCustomElement{
sp: SignaturePad;
sCanvas : HTMLCanvasElement;
attached(){
this.sp = new SignaturePad(this.sCanvas);
this.resize();
}
resize(){
.. //resizes the canvas appropriately
}
}
custom-dialog.ts
export class CustomDialog(){
...
saveClick(){
//Need to check here if signature was added (!SignatureInput.sp.isEmpty())
let sigExists = ??
if(sigExists){
this.dialogController.ok(true);
}
}
}
If you have <signature-input view-model.ref="mySignatureInput"></signature-input> in your custom-dialog.html, you should be able to call let sigExists = this.mySignatureInput.sp.isEmpty(); in your custom-dialog.ts.
I’m guessing khuongduybui meant view-model.ref, which should work fine for this.
However, perhaps you could try to intercept signature pad’s events onBegin() and/or onEnd() in your custom element, check isEmpty() there and assign the result to your own isEmpty property which you can decorate with @bindable?
That way you don’t need the viewmodel ref, and you can just bind to the property in your view using the custom element
Thanks. I had started looking at onBegin() and onEnd() to see if I could work something out that way. However, I realized I also needed a way to get the image to send to the server using toDataURL(). (Could probably do something similar to what you mentioned with isEmpty()) But view-model.ref worked great – so thanks for that!
Just keep in mind that you introduce a tight coupling in this case. You might want to think about an Event aggregator to loosen the coupling if that makes sense in your usecase