Question: how to use a resource in my (often external) object
Problem:
to use an image / font / etc in CSS / HTML, we need the URL of the file
if we directly get the URL of a added as an object’s resource, we can see that it contains the object’s row_id /resource?code=TEST_IMG&type=IMG&object=ObjectExternal&objid=2012
Knowing that row_id are not consistent from one instance to another, we cannot directly use this kind of URLs (obtained through click right on the resource > get image url)
General Solutions
calculating the URL on the external object’s javascript
calculating the URL server-side Java code and passing it to the external object’s script
using a disposition resource and directly use a /resource URL
passing the resource as a base64 String to the script / template (useful for HTML/PDF publications for example)
- External object : DemoTestOe
- Functions: set a read function to habilitate groups
- Resources
- HTML resource "HTML"
- Javascript resource "SCRIPT"
- Image resource "TEST_IMG
External object’s Java code
Reminder: ResponsiveExternalObject is a specialized external object type that basically builds a webpage base on the resources with codes HTML, CSS and SCRIPT and calls the SCRIPT’s render method
package com.simplicite.extobjects.Demo;
import com.simplicite.webapp.web.ResponsiveExternalObject;
public class DemoTestOe extends ResponsiveExternalObject {}
var DemoTestOe = (function() {
var render = () => {
getExternalObjectImageUrl('DemoTestOe','TEST_IMG',url=>$('#test-img-container').attr('src',url));
};
// callback-based tool to get the url for an external object's image resource
var getExternalObjectImageUrl = (objectName,imageCode,callback)=>$ui.getAjax().getExternalObject(oe=>callback($ui.getAjax().getResourceURL(imageCode,'IMG','ObjectExternal',oe.id)),objectName);
return { render: render };
})();