Serenity: Carry Values Between 2 forms

Created on 19 Feb 2018  路  8Comments  路  Source: serenity-is/Serenity

Hi,

I am having 2 dialog(parent-child). I have list of data in child dialog.I need to get that list of data to parent.
How can I achieve this?

Most helpful comment

an anonymous function should work

// child dilaog
export class DummyDialog extends Serenity.EntityDialog<DummyRow, any> {
   public returnData = (dataList: any[]) => void;

   private someMethod() {
      // here's where you invoke & return simple array to parent
      this.returnData({ [1, 2, 3, 4] });
   }
}

// parent dialog TS class

let dialog = new DummyDialog();
dialog.returnData = (dataList) => {
   // do something with the returned list of data.
};
dialog.Open();


All 8 comments

an anonymous function should work

// child dilaog
export class DummyDialog extends Serenity.EntityDialog<DummyRow, any> {
   public returnData = (dataList: any[]) => void;

   private someMethod() {
      // here's where you invoke & return simple array to parent
      this.returnData({ [1, 2, 3, 4] });
   }
}

// parent dialog TS class

let dialog = new DummyDialog();
dialog.returnData = (dataList) => {
   // do something with the returned list of data.
};
dialog.Open();


Or you can define 1 property in parent and in child use some event to set value for this property

Closing due to inactivity.

Hi @she-ll-b-rite,

I have set the parent dialog's field value in onDialogClose() of child dialog. on debugging I can get value in parent dialog field but it is not get reflected in UI.

//in OnDialogClose() of child dialog

let parentdialog = new ParentDialog();
parentdialog.form.dummyfield.value = this.dummyvalue[0];

while debugging I am getting value in parent dialog.form.dummyfield.value, but after child dialog gets closed the value is not set in dummy field of parent dialog.

Can you please help me on this?

hopefullly the code below makes clearer. what's happening is, the child dialog is created in the parent, and immediately after the 'returnData' variable is assigned an anonymous function (recall functions can be assigned to variables in Javascript & TS). The anonymous function is invoked in the child's 'onDialogClose' method, passing in the array of numbers. The key to this working is that the 'this.form.dummyfield' in the anonymous function is a reference to the field in the parent dialog even though it's been assigned to & invoked in the child dialog. This is true of any local variables created in the parent dialog and then referenced using 'this.' in the anonymous function.

// parent dialog
export class ParentDialog extends Serenity.EntityDialog<ParentRow, any> {

   methodToOpenChildDialog() {
       let dialog = new ChildDialog();
       dialog.returnData = (dataList) => {
           // assign the returned value to the field on the parent form.
           this.form.dummyfield.value = dataList[0];
           ...
       };
       dialog.Open();
   }
}

// child dialog
export class ChildDialog extends Serenity.EntityDialog<ChildRow, any> {
   public returnData = (dataList: number[]) => void;

   protected onDialogClose() {
      // here's where you invoke to return simple array to parent
      this.returnData([1, 2, 3, 4]);
      ...
   }
}

Thank you very much @she-ll-b-rite for your clear and patient explanation.
It is completely working fine.

http://www.albertgao.xyz/2016/08/11/how-to-declare-a-function-type-variable-in-typescript/

I needed to change the typescript to look like this in the child window:
public returnData:(dataList: number[]) => void;

It hard to support you if you dont let me know your case. You can find some method like getSaveEntity(), validateBeforeSave() or callback of saveResponse

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moostafaa picture moostafaa  路  3Comments

ahsansolution picture ahsansolution  路  3Comments

AmuthaKondusamy picture AmuthaKondusamy  路  3Comments

JohnRanger picture JohnRanger  路  3Comments

chintankukadiya18 picture chintankukadiya18  路  3Comments