diff --git a/packages/person-select/src/vpu-person-select.js b/packages/person-select/src/vpu-person-select.js index 417f442842dffb51740c61ecedae2d18ddb27967..67ebd862c9eba9542e62b17dd9fce88a15f3b377 100644 --- a/packages/person-select/src/vpu-person-select.js +++ b/packages/person-select/src/vpu-person-select.js @@ -57,9 +57,25 @@ class PersonSelect extends VPULitElementJQuery { // because the blur gets also fired when clicking in the selector setTimeout(() => {that.$select.select2('close')}, 500); }); + + // try an init when user-interface is loaded + that.initJSONLD(); }); } + initJSONLD() { + const that = this; + + JSONLD.initialize(this.entryPointUrl, function (jsonld) { + that.jsonld = jsonld; + that.active = true; + + // we need to poll because maybe the user interface isn't loaded yet + // Note: we need to call initSelect2() in a different function so we can access "this" inside initSelect2() + commonUtils.pollFunc(() => { return that.initSelect2(); }, 10000, 100); + }, {}, this.lang); + } + /** * Initializes the Select2 selector */ @@ -68,6 +84,10 @@ class PersonSelect extends VPULitElementJQuery { const $this = $(this); let lastResult = {}; + if (this.jsonld === null) { + return false; + } + // find the correct api url for a person const apiUrl = this.jsonld.getApiUrlForIdentifier("http://schema.org/Person"); // const apiUrl = this.jsonld.getApiUrlForEntityName("Event"); @@ -78,6 +98,11 @@ class PersonSelect extends VPULitElementJQuery { "text": "http://schema.org/name" }; + if (this.$select === null) { + return false; + } + + // we need to destroy Select2 before we can initialize it again if (this.$select && this.$select.hasClass('select2-hidden-accessible')) { this.$select.select2('destroy'); } @@ -182,7 +207,7 @@ class PersonSelect extends VPULitElementJQuery { }).catch(() => {}); } - return this.$select; + return true; } update(changedProperties) { @@ -204,13 +229,7 @@ class PersonSelect extends VPULitElementJQuery { this.ignoreValueUpdate = false; break; case "entryPointUrl": - const that = this; - - JSONLD.initialize(this.entryPointUrl, function (jsonld) { - that.jsonld = jsonld; - that.active = true; - that.$select = that.initSelect2(); - }, {}, that.lang); + this.initJSONLD(); break; } });