0

I made two Custom Controls named, "CustomControl_1" and "CustomControl_2".

In the render function of "CustomControl_1", how to access the value of "CustomControl_2"?

For example,

myapp.BrowseOrders.CustomControl_2_render = function (element, contentItem) {       
    $(element).text("Some Value");
};

myapp.BrowseOrders.CustomControl_1_render = function (element, contentItem) {       
    $(element).text( Value of CustomControl_2 ? );
};

1 Answers1

0

As with most things in development, you can achieve this in a number of different ways (there's always more than one way to skin a cat ;-).

The simplest way would be to cache the element you're interesting in against the screen object.

This cached element could then be accessed, as shown in the following example, by using the cached element (_$element_CustomControl_2):-

myapp.BrowseOrders.CustomControl_2_render = function (element, contentItem) {
    contentItem.screen._$element_CustomControl_2 = $(element);      
    $(element).text("Some Value");
};

myapp.BrowseOrders.CustomControl_1_render = function (element, contentItem) {       
    $(element).text("Value of CustomControl_2 is " + contentItem.screen._$element_CustomControl_2.text());
};

If the control you need to reference is rendered after the point you need to access its value, you can wrap the access code in a setTimeout to delay the access until after both elements have rendered.

I suspect in your case this will be required (as shown in the following extended example): -

myapp.BrowseOrders.CustomControl_2_render = function (element, contentItem) {
    contentItem.screen._$element_CustomControl_2 = $(element);      
    $(element).text("Some Value");
};

myapp.BrowseOrders.CustomControl_1_render = function (element, contentItem) {       
    setTimeout(function () {
        $(element).text("Value of CustomControl_2 is " + contentItem.screen._$element_CustomControl_2.text());
    });
};

This would occur if CustomControl2 appears after CustomControl1 in the LightSwitch screen content tree e.g.

Example of the LightSwitch screen content tree

The technicalities of the setTimeout approach are covered in the following stack overflow post: -

'Why is setTimeout(fn, 0) sometimes useful?'

Community
  • 1
  • 1
Chris Cook
  • 2,821
  • 1
  • 20
  • 32