Skip to content

Commit

Permalink
Removed JSONModel from GetRenderedView (updated all the related units).
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Nov 26, 2024
1 parent b7b2556 commit e78f525
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
interface

uses
System.Rtti;
TemplatePro, System.Rtti;

procedure TemplateProContextConfigure;

implementation

uses
TemplatePro, System.SysUtils;
System.SysUtils;

function MyHelper1(const aValue: TValue; const aParameters: TArray<string>): TValue;
function MyHelper1(const aValue: TValue; const aParameters: TArray<TFilterParameter>): TValue;
begin
Result := aValue.ToString + ' (I''m The MyHelper1)';
end;

function MyHelper2(const aValue: TValue; const aParameters: TArray<string>): TValue;
function MyHelper2(const aValue: TValue; const aParameters: TArray<TFilterParameter>): TValue;
begin
Result := aValue.ToString + ' (I''m The MyHelper2)';
end;
Expand Down
35 changes: 10 additions & 25 deletions sources/MVCFramework.View.Renderers.Mustache.pas
Original file line number Diff line number Diff line change
Expand Up @@ -219,38 +219,23 @@ procedure TMVCMustacheViewEngine.PrepareModels;
Exit;
end;

if Assigned(FJSONModel) and (not Assigned(ViewModel)) then
begin
// if only jsonmodel is <> nil then we take the "fast path"
FJSONModelAsString := FJSONModel.ToJSON(False);
end
else
begin
lSer := fSerializerPool.GetFromPool(True) as IMVCSerializer;
lSer := fSerializerPool.GetFromPool(True) as IMVCSerializer;
try
lJSONModel := TJsonObject.Create;
try
if Assigned(FJSONModel) then
begin
lJSONModel := FJSONModel.Clone as TJsonObject;
end
else
if Assigned(ViewModel) then
begin
lJSONModel := TJsonObject.Create;
end;
try
if Assigned(ViewModel) then
for DataObj in ViewModel do
begin
for DataObj in ViewModel do
begin
TMVCJsonDataObjectsSerializer(lSer).TValueToJSONObjectProperty(lJSONModel, DataObj.Key, DataObj.Value, TMVCSerializationType.stDefault, nil, nil);
end;
TMVCJsonDataObjectsSerializer(lSer).TValueToJSONObjectProperty(lJSONModel, DataObj.Key, DataObj.Value, TMVCSerializationType.stDefault, nil, nil);
end;
FJSONModelAsString := lJSONModel.ToJSON(False);
finally
lJSONModel.Free;
end;
FJSONModelAsString := lJSONModel.ToJSON(False);
finally
fSerializerPool.ReleaseToPool(lSer)
lJSONModel.Free;
end;
finally
fSerializerPool.ReleaseToPool(lSer)
end;
fModelPrepared := True;
end;
Expand Down
56 changes: 11 additions & 45 deletions sources/MVCFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,6 @@ TMVCController = class(TMVCRenderer)
function GetCurrentWebModule: TWebModule;
function GetViewModel: TMVCViewDataObject;
function GetRenderedView(const AViewNames: TArray<string>; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; virtual;
function GetRenderedView(const AViewNames: TArray<string>; const JSONModel: TJSONObject; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback = nil): string; overload; virtual;

/// <summary>
/// Normally used in OnBeforeControllerAction to define view headers automatically used by the Page method.
Expand Down Expand Up @@ -1271,7 +1270,6 @@ TMVCBaseViewEngine = class(TMVCBase)
FViewPath: string;
FDefaultViewFileExtension: string;
FUseViewCache: Boolean;
FJSONModel: TJSONObject;
FBeforeRenderCallback: TMVCSSVBeforeRenderCallback;
function GetRealFileName(const AViewName: string): string; virtual;
function IsCompiledVersionUpToDate(const AFileName, ACompiledFileName: string): Boolean; virtual; abstract;
Expand All @@ -1282,13 +1280,6 @@ TMVCBaseViewEngine = class(TMVCBase)
const AController: TMVCController;
const AViewModel: TMVCViewDataObject;
const AContentType: string); overload; virtual;
constructor Create(
const AEngine: TMVCEngine;
const AWebContext: TWebContext;
const AController: TMVCController;
const AViewModel: TMVCViewDataObject;
const AJSONModel: TJSONObject;
const AContentType: string); overload; virtual;
destructor Destroy; override;

procedure Execute(const ViewName: string; const Builder: TStringBuilder); virtual; abstract;
Expand Down Expand Up @@ -2353,7 +2344,13 @@ constructor TMVCErrorResponseItem.Create(const AMessage: string);
function TWebContext.GetLoggedUser: TUser;
begin
if not Assigned(FLoggedUser) then
begin
FLoggedUser := TUser.Create;
if SessionStarted then
begin
fLoggedUser.LoadFromSession(GetWebSession);
end;
end;
Result := FLoggedUser;
end;

Expand Down Expand Up @@ -2457,8 +2454,8 @@ procedure TWebContext.SessionStop(const ARaiseExceptionIfExpired: Boolean);
Cookie := FResponse.Cookies.Add;
Cookie.name := TMVCConstants.SESSION_TOKEN_NAME;

Cookie.Value := GUIDToString(TGUID.NewGuid) + 'invalid' + GUIDToString(TGUID.NewGuid);
Cookie.Expires := EncodeDate(1970, 1, 1);
Cookie.Value := '';
Cookie.Expires := EncodeDate(1979, 11, 4);
Cookie.Path := '/';

TMonitor.Enter(GlobalSessionList);
Expand Down Expand Up @@ -4195,13 +4192,13 @@ function TMVCController.GetIfNoneMatch: String;
Result := Context.Request.GetHeader('If-None-Match');
end;

function TMVCController.GetRenderedView(const AViewNames: TArray<string>; const JSONModel: TJSONObject; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
function TMVCController.GetRenderedView(const AViewNames: TArray<string>; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
var
lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringBuilder;
begin
lStrStream := TStringBuilder.Create;
try
lView := FEngine.ViewEngineClass.Create(Engine, Context, Self, FViewModel, JSONModel, ContentType);
lView := FEngine.ViewEngineClass.Create(Engine, Context, Self, FViewModel, ContentType);
try
lView.FBeforeRenderCallback := OnBeforeRenderCallback;
for lViewName in AViewNames do
Expand Down Expand Up @@ -4377,20 +4374,6 @@ function TMVCController.GetViewModel: TMVCViewDataObject;
Result := FViewModel;
end;

//function TMVCController.LoadView(const AViewNames: TArray<string>; const JSONModel: TJSONObject = nil): string;
//begin
// try
// Result := GetRenderedView(AViewNames, JSONModel);
// ResponseStream.Append(Result);
// except
// on E: Exception do
// begin
// Log.Error('[%s] %s', [E.Classname, E.Message], LOGGERPRO_TAG);
// raise;
// end;
// end;
//end;

procedure TMVCController.MVCControllerAfterCreate;
begin
{ Implement if need be. }
Expand All @@ -4417,7 +4400,7 @@ procedure TMVCController.OnBeforeAction(AContext: TWebContext; const AActionName

function TMVCController.Page(const AViewName: string; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
Result := GetRenderedView([AViewName], nil, OnBeforeRenderCallback);
Result := GetRenderedView([AViewName], OnBeforeRenderCallback);
end;

function TMVCController.Page(const AViewNames: TArray<string>; const UseCommonHeadersAndFooters: Boolean; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
Expand Down Expand Up @@ -4846,11 +4829,6 @@ procedure TMVCRenderer.Render<T>(const AStatusCode: Integer; const ACollection:
Render<T>(ACollection, AOwns, ASerializationAction);
end;

function TMVCController.GetRenderedView(const AViewNames: TArray<string>; const OnBeforeRenderCallback: TMVCSSVBeforeRenderCallback): string;
begin
Result := GetRenderedView(AViewNames, nil, OnBeforeRenderCallback);
end;

procedure TMVCRenderer.Render<T>(const ACollection: TObjectList<T>;
const ASerializationAction: TMVCSerializationAction<T>);
begin
Expand Down Expand Up @@ -5230,18 +5208,6 @@ constructor TMVCBaseViewEngine.Create(
FDefaultViewFileExtension := WebContext.Config[TMVCConfigKey.DefaultViewFileExtension];
end;

constructor TMVCBaseViewEngine.Create(
const AEngine: TMVCEngine;
const AWebContext: TWebContext;
const AController: TMVCController;
const AViewModel: TMVCViewDataObject;
const AJSONModel: TJSONObject;
const AContentType: string);
begin
Create(AEngine, AWebContext, AController, AViewModel, AContentType);
fJSONModel := AJSONModel;
end;

destructor TMVCBaseViewEngine.Destroy;
begin
inherited Destroy;
Expand Down
2 changes: 1 addition & 1 deletion unittests/general/Several/LiveServerTestU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ procedure TServerTest.TestCustomAuthLoginLogout;
lPass := false;
for lCookie in lRes.Cookies do
begin
if lCookie.Value.Contains('invalid') then
if lCookie.Value.IsEmpty then
begin
lPass := true;
Break;
Expand Down

0 comments on commit e78f525

Please sign in to comment.