Execute a function in the context of another cfc in ColdFusion, following import instructions

Background

I am trying to create a function componentFromJsonthat can restore a component graph from JSON. I made a simple approach when I use getMetaDatato search for component properties to create the correct types.

The function will be used as: comp = componentFromJson(json, 'RootComponentType')

Problem

The problem is that the type of properties is not necessarily fully qualified, as namespaces can be imported, as shown below.

<cfimport path="some.namespace.Test">

<cfcomponent>
    <cfproperty name="test" type="Test">
</cfcomponent>

When I try to execute createObject('Test')functions from the context componentFromJson, it obviously does not work, because the calling context has no import.

, factory invoke factory CFC, .

.

<cfscript>
    parentCmp = createObject('SomeCmp');
    parentCmp.createComponent = function (type) {
        return createObject(type);
    };
    childCmp = invoke(parentCmp, 'createComponent', { type = 'Test' });
</cfscript>

, , - CFF ColdFusion , , . , .

?

, - ? , ? , ColdFusion, .

+4
1

, , , ColdFusion ( ).

, , ColdFusion, coldfusion.runtime.TemplateProxy, coldfusion.runtime.CFPage, createObject.

Java:

<cfset host = new SomeComponent()>
<cfset pageField = createObject('java', 'coldfusion.runtime.TemplateProxy').getClass().getDeclaredField('page')>
<cfset pageField.setAccessible(true)>
<cfset page = pageField.get(host)>
<cfset test = page.createObject('Test')>
+1

Source: https://habr.com/ru/post/1652378/


All Articles