Added method Page, PageFragment, SetPagesCommonHeaders and SetPagesCommonFooters. Removed the old LoadViewFragment method.

This commit is contained in:
Daniele Teti 2023-10-10 18:17:44 +02:00
parent 7c3f301054
commit 9c70a6244c

View File

@ -777,6 +777,8 @@ type
private private
FViewModel: TMVCViewDataObject; FViewModel: TMVCViewDataObject;
FViewDataSets: TMVCViewDataSet; FViewDataSets: TMVCViewDataSet;
fPageHeaders: TArray<String>;
fPageFooters: TArray<String>;
function GetSession: TMVCWebSession; function GetSession: TMVCWebSession;
function GetViewData(const aModelName: string): TObject; function GetViewData(const aModelName: string): TObject;
function GetViewDataset(const aDataSetName: string): TDataSet; function GetViewDataset(const aDataSetName: string): TDataSet;
@ -799,6 +801,42 @@ type
function GetRenderedView(const AViewNames: TArray<string>): string; overload; virtual; function GetRenderedView(const AViewNames: TArray<string>): string; overload; virtual;
function GetRenderedView(const AViewNames: TArray<string>; const JSONModel: TJSONObject): string; overload; virtual; function GetRenderedView(const AViewNames: TArray<string>; const JSONModel: TJSONObject): string; overload; virtual;
/// <summary>
/// Normally used in OnBeforeControllerAction to define view headers automatically used by the Page method.
/// </summary>
procedure SetPagesCommonHeaders(const AViewNames: TArray<string>);
/// <summary>
/// Normally used in OnBeforeControllerAction to define view footers automatically used by the Page method.
/// </summary>
procedure SetPagesCommonFooters(const AViewNames: TArray<string>);
/// <summary>
/// Page calls GetRenderedView with sensible defaults.
/// Page method just concatenate -> commonheader_header_views + views + commonfooter_views
/// PageFragment ignore header and footer views
/// </summary>
function Page(const AViewNames: TArray<string>): string; overload; inline;
/// <summary>
/// Page calls GetRenderedView with sensible defaults.
/// Page method just concatenate -> commonheader_header_views + views + commonfooter_views
/// PageFragment ignore header and footer views
/// </summary>
function Page(const AViewNames: TArray<string>; const JSONModel: TJSONObject): string; overload; inline;
/// <summary>
/// PageFragment calls GetRenderedView.
/// PageFragment ignore header and footer views.
/// </summary>
function PageFragment(const AViewNames: TArray<string>): string; overload; inline;
/// <summary>
/// PageFragment calls GetRenderedView.
/// PageFragment ignore header and footer views.
/// </summary>
function PageFragment(const AViewNames: TArray<string>; const JSONModel: TJSONObject): string; overload; inline;
/// <summary> /// <summary>
/// Load mustache view located in TMVCConfigKey.ViewsPath /// Load mustache view located in TMVCConfigKey.ViewsPath
/// returns the rendered views and generates output using /// returns the rendered views and generates output using
@ -806,14 +844,6 @@ type
/// </summary> /// </summary>
function LoadView(const AViewNames: TArray<string>; const JSONModel: TJSONObject = nil): string; virtual; function LoadView(const AViewNames: TArray<string>; const JSONModel: TJSONObject = nil): string; virtual;
/// <summary>
/// Load a view fragment in the output render stream. The view fragment is appended to the
/// ResponseStream verbatim. No processing happens.
/// Useful when used with cache.
/// It is equivalent to <code>ResponseStream.Append(AViewFragment);</code>
/// </summary>
procedure LoadViewFragment(const AViewFragment: string);
function SessionAs<T: TMVCWebSession>: T; function SessionAs<T: TMVCWebSession>: T;
procedure RaiseSessionExpired; virtual; procedure RaiseSessionExpired; virtual;
@ -3672,6 +3702,8 @@ begin
FResponseStream := nil; FResponseStream := nil;
FViewModel := nil; FViewModel := nil;
FViewDataSets := nil; FViewDataSets := nil;
fPageHeaders := nil;
fPageFooters := nil;
end; end;
destructor TMVCController.Destroy; destructor TMVCController.Destroy;
@ -3823,11 +3855,6 @@ begin
end; end;
end; end;
procedure TMVCController.LoadViewFragment(const AViewFragment: string);
begin
ResponseStream.Append(AViewFragment);
end;
procedure TMVCController.MVCControllerAfterCreate; procedure TMVCController.MVCControllerAfterCreate;
begin begin
{ Implement if need be. } { Implement if need be. }
@ -3852,6 +3879,28 @@ begin
{ Implement if need be. } { Implement if need be. }
end; end;
function TMVCController.Page(const AViewNames: TArray<string>;
const JSONModel: TJSONObject): string;
begin
Result := GetRenderedView(fPageHeaders + AViewNames + fPageFooters, JSONModel);
end;
function TMVCController.PageFragment(const AViewNames: TArray<string>;
const JSONModel: TJSONObject): string;
begin
Result := GetRenderedView(AViewNames, JSONModel);
end;
function TMVCController.PageFragment(const AViewNames: TArray<string>): string;
begin
Result := GetRenderedView(AViewNames);
end;
function TMVCController.Page(const AViewNames: TArray<string>): string;
begin
Result := GetRenderedView(fPageHeaders + AViewNames + fPageFooters);
end;
procedure TMVCController.PushDataSetToView(const aModelName: string; const ADataSet: TDataSet); procedure TMVCController.PushDataSetToView(const aModelName: string; const ADataSet: TDataSet);
begin begin
GetViewDataSets.Add(aModelName, ADataSet); GetViewDataSets.Add(aModelName, ADataSet);
@ -4069,6 +4118,16 @@ begin
Context.Response.SetCustomHeader('ETag', GetSHA1HashFromString(Data)); Context.Response.SetCustomHeader('ETag', GetSHA1HashFromString(Data));
end; end;
procedure TMVCController.SetPagesCommonFooters(const AViewNames: TArray<string>);
begin
fPageFooters := AViewNames;
end;
procedure TMVCController.SetPagesCommonHeaders(const AViewNames: TArray<string>);
begin
fPageHeaders := AViewNames;
end;
procedure TMVCController.SetViewData(const aModelName: string; const Value: TObject); procedure TMVCController.SetViewData(const aModelName: string; const Value: TObject);
begin begin
GetViewModel.Add(aModelName, Value); GetViewModel.Add(aModelName, Value);