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.