Skip to content

Commit

Permalink
ViewData[] is now a TValue dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Oct 14, 2023
1 parent 9175963 commit ef6edd5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion sources/MVCFramework.Commons.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface
System.SysUtils,
System.SyncObjs,
System.IOUtils,
System.RTTI,
Data.DB,
IdGlobal,
IdCoderMIME,
Expand Down Expand Up @@ -517,7 +518,7 @@ TMVCStringObjectDictionary<T: class> = class
procedure Add(const Name: string; Value: T);
end;

TMVCViewDataObject = class(TObjectDictionary<string, TObject>)
TMVCViewDataObject = class(TObjectDictionary<string, TValue>)
private
{ private declarations }
protected
Expand Down
25 changes: 13 additions & 12 deletions sources/MVCFramework.View.Renderers.Mustache.pas
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ interface

uses
MVCFramework, System.SysUtils, System.Generics.Collections,
MVCFramework.Commons, System.IOUtils, System.Classes, Data.DB, SynMustache, SynCommons;
MVCFramework.Commons, System.IOUtils, System.RTTI,
System.Classes, Data.DB, SynMustache, SynCommons;

type
{ This class implements the mustache view engine for server side views }
Expand Down Expand Up @@ -204,8 +205,7 @@ procedure TMVCMustacheViewEngine.LoadPartials;

procedure TMVCMustacheViewEngine.PrepareModels;
var
lList: IMVCList;
DataObj: TPair<string, TObject>;
DataObj: TPair<string, TValue>;
lDSPair: TPair<string, TDataSet>;
lSer: TMVCJsonDataObjectsSerializer;
lJSONModel: TJsonObject;
Expand Down Expand Up @@ -233,15 +233,16 @@ procedure TMVCMustacheViewEngine.PrepareModels;
begin
for DataObj in ViewModel do
begin
lList := TDuckTypedList.Wrap(DataObj.Value);
if lList <> nil then
begin
lSer.ListToJsonArray(lList, lJSONModel.A[DataObj.Key], TMVCSerializationType.stProperties, nil);
end
else
begin
lSer.ObjectToJsonObject(DataObj.Value, lJSONModel.O[DataObj.Key], TMVCSerializationType.stProperties, nil);
end;
lSer.TValueToJSONObjectProperty(lJSONModel, DataObj.Key, DataObj.Value, TMVCSerializationType.stDefault, nil, nil);
// lList := TDuckTypedList.Wrap(DataObj.Value);
// if lList <> nil then
// begin
// lSer.ListToJsonArray(lList, lJSONModel.A[DataObj.Key], TMVCSerializationType.stProperties, nil);
// end
// else
// begin
// lSer.ObjectToJsonObject(DataObj.Value, lJSONModel.O[DataObj.Key], TMVCSerializationType.stProperties, nil);
// end;
end;
end;

Expand Down
10 changes: 5 additions & 5 deletions sources/MVCFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,9 @@ TMVCController = class(TMVCRenderer)
fPageHeaders: TArray<String>;
fPageFooters: TArray<String>;
function GetSession: TMVCWebSession;
function GetViewData(const aModelName: string): TObject;
function GetViewData(const aModelName: string): TValue;
function GetViewDataset(const aDataSetName: string): TDataSet;
procedure SetViewData(const aModelName: string; const Value: TObject);
procedure SetViewData(const aModelName: string; const Value: TValue);
procedure SetViewDataset(const aDataSetName: string; const Value: TDataSet);
protected const
CLIENTID_KEY = '__clientid';
Expand Down Expand Up @@ -863,7 +863,7 @@ TMVCController = class(TMVCRenderer)
procedure PushDataSetToView(const aModelName: string; const ADataSet: TDataSet);
deprecated 'Use "ViewDataSet"';

property ViewData[const aModelName: string]: TObject read GetViewData write SetViewData;
property ViewData[const aModelName: string]: TValue read GetViewData write SetViewData;
property ViewDataset[const aDataSetName: string]: TDataSet read GetViewDataset
write SetViewDataset;

Expand Down Expand Up @@ -3815,7 +3815,7 @@ class procedure TMVCRenderer.InternalRenderMVCResponse(

end;

function TMVCController.GetViewData(const aModelName: string): TObject;
function TMVCController.GetViewData(const aModelName: string): TValue;
begin
if not FViewModel.TryGetValue(aModelName, Result) then
Result := nil;
Expand Down Expand Up @@ -4128,7 +4128,7 @@ procedure TMVCController.SetPagesCommonHeaders(const AViewNames: TArray<string>)
fPageHeaders := AViewNames;
end;

procedure TMVCController.SetViewData(const aModelName: string; const Value: TObject);
procedure TMVCController.SetViewData(const aModelName: string; const Value: TValue);
begin
GetViewModel.Add(aModelName, Value);
end;
Expand Down

0 comments on commit ef6edd5

Please sign in to comment.