Page 1 of 2

IWCGJQFrameHelperUnit

PostPosted: 27 Sep 2013 16:15
by JohnFSklavounos
Hello Everyone,

I find myself creating frames for dynamic inclusion into multiple forms so I've created this helper unit to make life a bit easier. Please feel free to use and improve it. If you make it better, please add your name, date and re-post it.

All my best,
John

delphi code
unit IWCGJQFrameHelperUnit;

/// =================================================================================================
///
/// Author: John F. Sklavounos
/// Date: 2013-09-26
///
/// Ownership:
/// You are free to use and distribute this code as you see fit. I only ask that improvements be
/// returned to the community at http://www.cgdevtools.com/cgforum/viewforum.php?f=109
///
/// Abstract:
/// This helper routine helps me keep the code straightforward for creating and destroying custom
/// frames that live inside a TIWCGJQRegion.
///
/// The idea for this came from the CG Developer Team in response to this post:
/// http://www.cgdevtools.com/cgforum/viewtopic.php?f=3&t=723
///
/// Basically I didn't want to rewrite or copy/paste this code into more than one place. I also
/// cleaned it up so there is only one function call to create a frame, regardless of whether or
/// not it's called from an OnCreate or a fired event.
///
/// Notes:
/// I would like to modify the CreateCGJQFrame so the Owner could be the common ancestor for
/// all possible components that can contain a frame. In other words, call the create using a
/// TIWAppForm, TIWCGJQRegion, TIWCGJQTab, TIWRegion, etc... I can't seem to find a common
/// ancestorso it may be that I'll have to create an override for each, but I'm hoping someone
/// will know, modify the code and return it to the community.
/// =================================================================================================

interface

uses
SysUtils, Classes, IWCGJQRegion, IWCGFrame, IWHTMLContainer, IWCGJQCommon, IWCGAsyncRender;

type TIWCGJQFrameHelper = class
class procedure CreateCGJQFrame(pOwner: TIWCGJQRegion; pFrameClass: TIWCGFrameClass; pParams: TStringList; var pFrame: TIWCGJQFrame);
class procedure DestroyCGJQFrame(var pIWCGJQFrame: TIWCGJQFrame);
end;

implementation

class procedure TIWCGJQFrameHelper.CreateCGJQFrame(pOwner: TIWCGJQRegion; pFrameClass: TIWCGFrameClass; pParams: TStringList; var pFrame: TIWCGJQFrame);
begin
///
/// Always destroy the existing frame when creating a new one. If the programmer wants more than one,
/// then there should be multiple frame variables and creates/destroys or an array of frames.
///
if Assigned(pFrame) then DestroyCGJQFrame(pFrame);
///
if CGIsCallBackProcessing then CGCallBackDisableAjaxResponse;
///
pFrame := pFrameClass.Create(pOwner);
///
pFrame.Name := pOwner.Name + '_' + pFrame.Name;
pFrame.Parent := pOwner;
///
/// If pParams is assigned (not nil) then we must complete the Ajax processing
///
if Assigned(pParams) then begin
pFrame.ProcessCommand(0, pParams);
if pOwner.WebApplication.CallBackProcessing then CGCallBackEnableAjaxResponse;
RenderRegionAsync(pOwner, False, True);
end;
end;

class procedure TIWCGJQFrameHelper.DestroyCGJQFrame(var pIWCGJQFrame: TIWCGJQFrame);
begin
FreeAndNil(pIWCGJQFrame);
end;

end.

Re: IWCGJQFrameHelperUnit

PostPosted: 27 Sep 2013 21:45
by Jorge Sousa
Johnny

WOW!!!

This is awesome!

Can we use it internally right?

Thanks a lot!

Best Regards

Re: IWCGJQFrameHelperUnit

PostPosted: 27 Sep 2013 22:29
by JohnFSklavounos
Of course, I would be honored if you incorporated it somehow... :D

Best,
John

Re: IWCGJQFrameHelperUnit

PostPosted: 27 Sep 2013 23:36
by Jorge Sousa
Hi John

The common ancestor is TIWBaseContainer or TIWContainer? the same in ARegion parameter in RenderAnsyncRegion, I can't check right now, I'm cloning a Samsung SSD PRO 512 GB yay!

Best Regards

Re: IWCGJQFrameHelperUnit

PostPosted: 27 Sep 2013 23:53
by Jorge Sousa
ARegion: TIWHTMLContainer

Re: IWCGJQFrameHelperUnit

PostPosted: 28 Sep 2013 15:45
by JohnFSklavounos
I'll make the mods in a bit and test with the ancestor class. Thanks! :D

Re: IWCGJQFrameHelperUnit

PostPosted: 28 Sep 2013 22:03
by JohnFSklavounos
OK, Here's the updated code and we're looking good... :D We can now dynamically create and destroy frames in a bunch more containers than before. Thanks to CGDevTools for the insight into the ancestor classes.

delphi code
unit IWCGJQFrameHelperUnit;

/// =================================================================================================
///
/// Author: John F. Sklavounos, CGDevTools
/// Date: 2013-09-26
///
/// Ownership:
/// You are free to use and distribute this code as you see fit. I only ask that improvements be
/// returned to the community at http://www.cgdevtools.com/cgforum/viewforum.php?f=109
///
/// Abstract:
/// This helper routine helps me keep the code straightforward for creating and destroying custom
/// frames that live inside a TIWCGJQRegion.
///
/// The idea for this came from the CG Developer Team in response to this post:
/// http://www.cgdevtools.com/cgforum/viewtopic.php?f=3&t=723
///
/// Basically I didn't want to rewrite or copy/paste this code into more than one place. I also
/// cleaned it up so there is only one function call to create a frame, regardless of whether or
/// not it's called from an OnCreate or a fired event.
/// =================================================================================================

interface

uses
SysUtils, Classes, IWCGJQRegion, IWCGFrame, IWHTMLContainer, IWCGJQCommon, IWCGAsyncRender;

type TIWCGJQFrameHelper = class
class procedure CreateCGJQFrame(pContainer: TIWHTMLContainer; pFrameClass: TIWCGFrameClass; pParams: TStringList; var pFrame: TIWCGJQFrame);
class procedure DestroyCGJQFrame(var pIWCGJQFrame: TIWCGJQFrame);
end;

implementation

class procedure TIWCGJQFrameHelper.CreateCGJQFrame(pContainer: TIWHTMLContainer; pFrameClass: TIWCGFrameClass; pParams: TStringList; var pFrame: TIWCGJQFrame);
begin
///
/// Always destroy the existing frame when creating a new one. If the programmer wants more than one,
/// then there should be multiple frame variables and creates/destroys or an array of frames.
///
if Assigned(pFrame) then DestroyCGJQFrame(pFrame);
///
if CGIsCallBackProcessing then CGCallBackDisableAjaxResponse;
///
pFrame := pFrameClass.Create(pContainer.Owner);
///
pFrame.Parent := pContainer;
///
/// If pParams is assigned (not nil) then we must complete the Ajax processing
///
if Assigned(pParams) then begin
pFrame.ProcessCommand(0, pParams);
if pContainer.OwnerForm.WebApplication.CallBackProcessing then CGCallBackEnableAjaxResponse;
RenderRegionAsync(pContainer, False, True);
end;
end;

class procedure TIWCGJQFrameHelper.DestroyCGJQFrame(var pIWCGJQFrame: TIWCGJQFrame);
begin
FreeAndNil(pIWCGJQFrame);
end;

end.

Re: IWCGJQFrameHelperUnit

PostPosted: 25 Feb 2016 20:08
by cleversonviana
How to do this using the Mobile components ?

I mean, I want to create a Mobile Frame dynamic inside another frame in my main page.

Re: IWCGJQFrameHelperUnit

PostPosted: 26 Feb 2016 10:00
by Alexander Bulei
Hi cleversonviana,

You need use the same method as in desktop.
Search for RenderRegionAsync or AjaxReRender methods in our forum.
Also, check the code of JohnFSklavounos.

Best Regards.

Re: IWCGJQFrameHelperUnit

PostPosted: 20 Apr 2016 23:19
by scoluccia
Hi
if I write your code I get "webapplication.isCallBack must be False Error!"
it works only if I write:
if CGIsCallBackProcessing then CGCallBackEnableAjaxResponse;
instead of
if pContainer.OwnerForm.WebApplication.CallBackProcessing then CGCallBackEnableAjaxResponse;